Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mq_notify.c « linuxthreads « linux « sys « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46492e470120360fcf8e82880f3ae8899bcae69a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Copyright 2002, Red Hat Inc. */

#include <mqueue.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <string.h>

#include "internals.h"
#include <sys/lock.h>

#include "mqlocal.h"

static void *mq_notify_process (void *);

void
__cleanup_mq_notify (struct libc_mq *info)
{
  struct sembuf sb4 = {4, 1, 0};
  /* kill notification thread and allow other processes to set a notification */
  pthread_cancel ((pthread_t)info->th);
  semop (info->semid, &sb4, 1);
}
  
static void *
mq_notify_process (void *arg)
{
  struct libc_mq *info = (struct libc_mq *)arg;
  struct sembuf sb3[2] = {{3, 0, 0}, {5, 0, 0}};
  struct sembuf sb4 = {4, 1, 0};
  int rc;

  /* wait until queue is empty */
  while (!(rc = semop (info->semid, sb3, 1)) && errno == EINTR)
    /* empty */ ;

  if (!rc)
    {
      /* now wait until there are 0 readers and the queue has something in it */
      sb3[0].sem_op = -1;
      while (!(rc = semop (info->semid, sb3, 2)) && errno == EINTR)
	/* empty */ ;
      /* restore value since we have not actually performed a read */
      sb3[0].sem_op = 1;
      semop (info->semid, sb3, 1);
      /* perform desired notification - either run function in this thread or pass signal */
      if (!rc)
	{
	  if (info->sigevent->sigev_notify == SIGEV_SIGNAL)
	    raise (info->sigevent->sigev_signo);
	  else if (info->sigevent->sigev_notify == SIGEV_THREAD)
	    info->sigevent->sigev_notify_function (info->sigevent->sigev_value);
	  /* allow other processes to now mq_notify */
	  semop (info->semid, &sb4, 1);
	}
    }
  pthread_exit (NULL);
}

int
mq_notify (mqd_t msgid, const struct sigevent *notification)
{
  struct libc_mq *info;
  struct sembuf sb4 = {4, -1, IPC_NOWAIT};
  int rc;
  pthread_attr_t *attr = NULL;

  info = __find_mq (msgid);

  if (info == NULL)
    {
      errno = EBADF;
      return -1;
    }

  /* get notification lock */
  rc = semop (info->semid, &sb4, 1);

  if (rc == -1)
    {
      errno = EBUSY;
      return -1;
    }

  /* to get the notification running we use a pthread - if the user has requested
     an action in a pthread, we use the user's attributes when setting up the thread */
  info->sigevent = (struct sigevent *)notification;
  if (info->sigevent->sigev_notify == SIGEV_THREAD)
    attr = (pthread_attr_t *)info->sigevent->sigev_notify_attributes;
  rc = pthread_create ((pthread_t *)&info->th, attr, mq_notify_process, (void *)info);

  if (rc != 0)
    rc = -1;
  else
    info->cleanup_notify = &__cleanup_mq_notify;

  return rc;
}