OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [sys/] [linux/] [mq_receive.c] - Blame information for rev 816

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/* Copyright 2002, Red Hat Inc. */
2
 
3
#include <mqueue.h>
4
#include <fcntl.h>
5
#include <errno.h>
6
#include <sys/ipc.h>
7
#include <sys/sem.h>
8
#include <string.h>
9
#define _LIBC 1
10
#include <sys/lock.h>
11
#undef _LIBC
12
 
13
#include "mqlocal.h"
14
 
15
__LOCK_INIT(static, mq_rdbuf_lock);
16
 
17
ssize_t
18
mq_receive (mqd_t msgid, char *msg, size_t msg_len, unsigned int *msg_prio)
19
{
20
  struct libc_mq *info;
21
  struct sembuf sb2 = {2, 1, 0};
22
  struct sembuf sb3 = {3, -1, IPC_NOWAIT};
23
  struct sembuf sb5 = {5, 1, IPC_NOWAIT};
24
  ssize_t num_bytes;
25
  int ipcflag;
26
 
27
  info = __find_mq (msgid);
28
 
29
  if (info == NULL || (info->oflag & O_ACCMODE) == O_WRONLY)
30
    {
31
      errno = EBADF;
32
      return -1;
33
    }
34
 
35
  if (msg_len < info->attr->mq_msgsize)
36
    {
37
      errno = EMSGSIZE;
38
      return -1;
39
    }
40
 
41
  __lock_acquire (mq_rdbuf_lock);
42
 
43
  ipcflag = (info->attr->mq_flags & O_NONBLOCK) ? IPC_NOWAIT : 0;
44
 
45
  semop (info->semid, &sb5, 1); /* increase number of readers */
46
  num_bytes = msgrcv (info->msgqid, info->rdbuf, msg_len, -MQ_PRIO_MAX, ipcflag);
47
  sb5.sem_op = -1;
48
  semop (info->semid, &sb5, 1); /* decrease number of readers */
49
 
50
  if (num_bytes != (ssize_t)-1)
51
    {
52
      semop (info->semid, &sb2, 1); /* add one to messages left to write */
53
      semop (info->semid, &sb3, 1); /* subtract one from messages to read */
54
      memcpy (msg, info->rdbuf->text, num_bytes);
55
      if (msg_prio != NULL)
56
        *msg_prio = MQ_PRIO_MAX - info->rdbuf->type;
57
    }
58
 
59
  __lock_release (mq_rdbuf_lock);
60
  return num_bytes;
61
}
62
 
63
 
64
 
65
 
66
 
67
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.