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 | |
---|
32 | extern int semid; |
---|
33 | |
---|
34 | /* |
---|
35 | * Shm_Initialize_lock |
---|
36 | * |
---|
37 | * Initialize the lock for the specified locked queue. |
---|
38 | */ |
---|
39 | |
---|
40 | void Shm_Initialize_lock( |
---|
41 | Shm_Locked_queue_Control *lq_cb |
---|
42 | ) |
---|
43 | { |
---|
44 | lq_cb->lock = lq_cb - Shm_Locked_queues; |
---|
45 | } |
---|
46 | |
---|
47 | /* Shm_Lock( &lq_cb ) |
---|
48 | * |
---|
49 | * This shared memory locked queue support routine locks the |
---|
50 | * specified locked queue. It disables interrupts to prevent |
---|
51 | * a deadlock condition. |
---|
52 | */ |
---|
53 | |
---|
54 | void Shm_Lock( |
---|
55 | Shm_Locked_queue_Control *lq_cb |
---|
56 | ) |
---|
57 | { |
---|
58 | rtems_unsigned32 isr_level; |
---|
59 | struct sembuf sb; |
---|
60 | int status; |
---|
61 | |
---|
62 | sb.sem_num = lq_cb->lock; |
---|
63 | sb.sem_op = -1; |
---|
64 | sb.sem_flg = 0; |
---|
65 | |
---|
66 | rtems_interrupt_disable( isr_level ); |
---|
67 | |
---|
68 | Shm_isrstat = isr_level; |
---|
69 | |
---|
70 | while (1) { |
---|
71 | status = semop(semid, &sb, 1); |
---|
72 | if ( status >= 0 ) |
---|
73 | break; |
---|
74 | if ( status == -1 ) { |
---|
75 | fix_syscall_errno(); /* in case of newlib */ |
---|
76 | if (errno == EINTR) |
---|
77 | continue; |
---|
78 | perror("shm lock"); |
---|
79 | rtems_fatal_error_occurred(RTEMS_UNSATISFIED); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /* |
---|
85 | * Shm_Unlock |
---|
86 | * |
---|
87 | * Unlock the lock for the specified locked queue. |
---|
88 | */ |
---|
89 | |
---|
90 | void 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 | sb.sem_num = lq_cb->lock; |
---|
99 | sb.sem_op = 1; |
---|
100 | sb.sem_flg = 0; |
---|
101 | |
---|
102 | while (1) { |
---|
103 | status = semop(semid, &sb, 1); |
---|
104 | if ( status >= 0 ) |
---|
105 | break; |
---|
106 | |
---|
107 | if ( status == -1 ) { |
---|
108 | fix_syscall_errno(); /* in case of newlib */ |
---|
109 | if (errno == EINTR) |
---|
110 | continue; |
---|
111 | perror("shm unlock"); |
---|
112 | rtems_fatal_error_occurred(RTEMS_UNSATISFIED); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | isr_level = Shm_isrstat; |
---|
117 | rtems_interrupt_enable( isr_level ); |
---|
118 | } |
---|