6136018c883a7878af57a95134b1ff2353f5c745
[platform/core/appfw/notification-service.git] / service_common.h
1 /*
2  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 enum tcb_type {
18         TCB_CLIENT_TYPE_APP     = 0x00,
19         TCB_CLIENT_TYPE_SERVICE = 0x01,
20         TCB_CLIENT_TYPE_UNKNOWN = 0xff,
21 };
22
23 struct tcb;
24 struct service_context;
25 struct service_event_item;
26
27 extern int tcb_fd(struct tcb *tcb);
28 extern struct service_context *tcb_svc_ctx(struct tcb *tcb);
29 extern int tcb_client_type(struct tcb *tcb);
30 extern int tcb_client_type_set(struct tcb *tcb, enum tcb_type type);
31
32 extern struct service_context *service_common_create(const char *addr, int (*service_thread_main)(struct tcb *tcb, struct packet *packet, void *data), void *data);
33 extern int service_common_destroy(struct service_context *svc_ctx);
34
35 extern int service_common_multicast_packet(struct tcb *tcb, struct packet *packet, int type);
36 extern int service_common_unicast_packet(struct tcb *tcb, struct packet *packet);
37
38 extern struct service_event_item *service_common_add_timer(struct service_context *svc_ctx, double timer, int (*timer_cb)(struct service_context *svc_cx, void *data), void *data);
39 extern int service_common_del_timer(struct service_context *svc_ctx, struct service_event_item *item);
40
41 #define CRITICAL_SECTION_BEGIN(handle) \
42 do { \
43         int ret; \
44         ret = pthread_mutex_lock(handle); \
45         if (ret != 0) \
46                 fprintf(stderr, "Failed to lock: %s\n", strerror(ret)); \
47 } while (0)
48
49 #define CRITICAL_SECTION_END(handle) \
50 do { \
51         int ret; \
52         ret = pthread_mutex_unlock(handle); \
53         if (ret != 0) \
54                 fprintf(stderr, "Failed to unlock: %s\n", strerror(ret)); \
55 } while (0)
56
57 #define CANCEL_SECTION_BEGIN() do { \
58         int ret; \
59         ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); \
60         if (ret != 0) \
61                 fprintf(stderr, "Unable to set cancelate state: %s\n", strerror(ret)); \
62 } while (0)
63
64 #define CANCEL_SECTION_END() do { \
65         int ret; \
66         ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); \
67         if (ret != 0) \
68                 fprintf(stderr, "Unable to set cancelate state: %s\n", strerror(ret)); \
69 } while (0)
70
71 #define CLOSE_PIPE(p)   do { \
72         int status; \
73         status = close(p[PIPE_READ]); \
74         if (status < 0) \
75                 fprintf(stderr, "close: %s\n", strerror(errno)); \
76         status = close(p[PIPE_WRITE]); \
77         if (status < 0) \
78                 fprintf(stderr, "close: %s\n", strerror(errno)); \
79 } while (0)
80
81 #define PIPE_READ 0
82 #define PIPE_WRITE 1
83 #define PIPE_MAX 2
84
85 /* End of a file */