Fix the list emptying logic.
[platform/core/appfw/notification-service.git] / service_common.h
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 enum tcb_type {
17         TCB_CLIENT_TYPE_APP     = 0x00,
18         TCB_CLIENT_TYPE_SERVICE = 0x01,
19         TCB_CLIENT_TYPE_UNKNOWN = 0xff,
20 };
21
22 struct tcb;
23 struct service_context;
24 struct service_event_item;
25
26 extern int tcb_fd(struct tcb *tcb);
27 extern struct service_context *tcb_svc_ctx(struct tcb *tcb);
28 extern int tcb_client_type(struct tcb *tcb);
29 extern int tcb_client_type_set(struct tcb *tcb, enum tcb_type type);
30
31 extern struct service_context *service_common_create(const char *addr, int (*service_thread_main)(struct tcb *tcb, struct packet *packet, void *data), void *data);
32 extern int service_common_destroy(struct service_context *svc_ctx);
33
34 extern int service_common_multicast_packet(struct tcb *tcb, struct packet *packet, int type);
35 extern int service_common_unicast_packet(struct tcb *tcb, struct packet *packet);
36
37 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);
38 extern int service_common_del_timer(struct service_context *svc_ctx, struct service_event_item *item);
39
40 #define CRITICAL_SECTION_BEGIN(handle) \
41 do { \
42         int ret; \
43         ret = pthread_mutex_lock(handle); \
44         if (ret != 0) \
45                 fprintf(stderr, "Failed to lock: %s\n", strerror(ret)); \
46 } while (0)
47
48 #define CRITICAL_SECTION_END(handle) \
49 do { \
50         int ret; \
51         ret = pthread_mutex_unlock(handle); \
52         if (ret != 0) \
53                 fprintf(stderr, "Failed to unlock: %s\n", strerror(ret)); \
54 } while (0)
55
56 #define CANCEL_SECTION_BEGIN() do { \
57         int ret; \
58         ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); \
59         if (ret != 0) \
60                 fprintf(stderr, "Unable to set cancelate state: %s\n", strerror(ret)); \
61 } while (0)
62
63 #define CANCEL_SECTION_END() do { \
64         int ret; \
65         ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); \
66         if (ret != 0) \
67                 fprintf(stderr, "Unable to set cancelate state: %s\n", strerror(ret)); \
68 } while (0)
69
70 #define CLOSE_PIPE(p)   do { \
71         int status; \
72         status = close(p[PIPE_READ]); \
73         if (status < 0) \
74                 fprintf(stderr, "close: %s\n", strerror(errno)); \
75         status = close(p[PIPE_WRITE]); \
76         if (status < 0) \
77                 fprintf(stderr, "close: %s\n", strerror(errno)); \
78 } while (0)
79
80 #define PIPE_READ 0
81 #define PIPE_WRITE 1
82 #define PIPE_MAX 2
83
84 /* End of a file */