source: rtems/c/src/lib/libbsp/unix/posix/shmsupp/lock.c @ f67ad3d

4.104.114.84.95
Last change on this file since f67ad3d was f67ad3d, checked in by Joel Sherrill <joel.sherrill@…>, on Jul 13, 1995 at 8:08:53 PM

all built successfully after merge

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*  Shared Memory Lock Routines
2 *
3 *  This shared memory locked queue support routine need to be
4 *  able to lock the specified locked queue.  Interrupts are
5 *  disabled while the queue is locked to prevent preemption
6 *  and deadlock when two tasks poll for the same lock.
7 *  previous level.
8 *
9 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
10 *  On-Line Applications Research Corporation (OAR).
11 *  All rights assigned to U.S. Government, 1994.
12 *
13 *  This material may be reproduced by or for the U.S. Government pursuant
14 *  to the copyright license under the clause at DFARS 252.227-7013.  This
15 *  notice must appear in all copies of this file and its derivatives.
16 *
17 *  $Id$
18 */
19
20#include <rtems.h>
21
22#include <bsp.h>
23#include <shm.h>
24
25#include <errno.h>
26#include <stdio.h>
27#include <unistd.h>
28#include <sys/ipc.h>
29#include <sys/shm.h>
30#include <sys/sem.h>
31
32extern int      semid;
33
34/*
35 *  Shm_Initialize_lock
36 *
37 *  Initialize the lock for the specified locked queue.
38 */
39
40void Shm_Initialize_lock(
41  Shm_Locked_queue_Control *lq_cb
42)
43{
44  lq_cb->lock = 
45    ( lq_cb - Shm_Locked_queues ) / sizeof( Shm_Locked_queue_Control );
46}
47
48/*  Shm_Lock( &lq_cb )
49 *
50 *  This shared memory locked queue support routine locks the
51 *  specified locked queue.  It disables interrupts to prevent
52 *  a deadlock condition.
53 */
54
55void Shm_Lock(
56  Shm_Locked_queue_Control *lq_cb
57)
58{
59    rtems_unsigned32   isr_level;
60    struct sembuf      sb;
61    int                status;
62
63    isr_level = 0;
64
65    sb.sem_num = lq_cb->lock;
66    sb.sem_op  = -1;
67    sb.sem_flg = 0;
68    rtems_interrupt_disable( isr_level );
69
70    Shm_isrstat = isr_level;
71
72    while (1) {
73      status = semop(semid, &sb, 1);
74      if ( status == 0 )
75        break;
76      if ( status == -1 && errno == EINTR )
77        continue;
78
79      fprintf( stderr, "shm lock(%d %d)\n", status, errno );
80      exit( 0 );
81    }
82}
83
84/*
85 *  Shm_Unlock
86 *
87 *  Unlock the lock for the specified locked queue.
88 */
89
90void Shm_Unlock(
91  Shm_Locked_queue_Control *lq_cb
92)
93{
94    rtems_unsigned32   isr_level;
95    struct sembuf      sb;
96    int                status;
97
98    isr_level = 0;
99
100    sb.sem_num = lq_cb->lock;
101    sb.sem_op  = 1;
102    sb.sem_flg = 0;
103
104    while (1) {
105      status = semop(semid, &sb, 1);
106      if ( status == 0 )
107        break;
108      if ( status == -1 && errno == EINTR )
109        continue;
110
111      fprintf( stderr, "shm unlock(%d %d)\n", status, errno );
112      exit( 0 );
113    }
114
115    isr_level = Shm_isrstat;
116    rtems_interrupt_enable( isr_level );
117}
Note: See TracBrowser for help on using the repository browser.