1 /* Copyright 2002, Red Hat Inc. */
10 #include "internals.h"
15 static void *mq_notify_process (void *);
18 __cleanup_mq_notify (struct libc_mq *info)
20 struct sembuf sb4 = {4, 1, 0};
21 /* kill notification thread and allow other processes to set a notification */
22 pthread_cancel ((pthread_t)info->th);
23 semop (info->semid, &sb4, 1);
27 mq_notify_process (void *arg)
29 struct libc_mq *info = (struct libc_mq *)arg;
30 struct sembuf sb3[2] = {{3, 0, 0}, {5, 0, 0}};
31 struct sembuf sb4 = {4, 1, 0};
34 /* wait until queue is empty */
35 while (!(rc = semop (info->semid, sb3, 1)) && errno == EINTR)
40 /* now wait until there are 0 readers and the queue has something in it */
42 while (!(rc = semop (info->semid, sb3, 2)) && errno == EINTR)
44 /* restore value since we have not actually performed a read */
46 semop (info->semid, sb3, 1);
47 /* perform desired notification - either run function in this thread or pass signal */
50 if (info->sigevent->sigev_notify == SIGEV_SIGNAL)
51 raise (info->sigevent->sigev_signo);
52 else if (info->sigevent->sigev_notify == SIGEV_THREAD)
53 info->sigevent->sigev_notify_function (info->sigevent->sigev_value);
54 /* allow other processes to now mq_notify */
55 semop (info->semid, &sb4, 1);
62 mq_notify (mqd_t msgid, const struct sigevent *notification)
65 struct sembuf sb4 = {4, -1, IPC_NOWAIT};
67 pthread_attr_t *attr = NULL;
69 info = __find_mq (msgid);
77 /* get notification lock */
78 rc = semop (info->semid, &sb4, 1);
86 /* to get the notification running we use a pthread - if the user has requested
87 an action in a pthread, we use the user's attributes when setting up the thread */
88 info->sigevent = (struct sigevent *)notification;
89 if (info->sigevent->sigev_notify == SIGEV_THREAD)
90 attr = (pthread_attr_t *)info->sigevent->sigev_notify_attributes;
91 rc = pthread_create ((pthread_t *)&info->th, attr, mq_notify_process, (void *)info);
96 info->cleanup_notify = &__cleanup_mq_notify;