aboutsummaryrefslogtreecommitdiff
path: root/src/backend/port/beos/sem.c
blob: d26369b3ee8dc4a2514be2af9f0c56350749a184 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*-------------------------------------------------------------------------
 *
 * sem.c
 *	  BeOS System V Semaphores Emulation
 *
 * Copyright (c) 1999-2000, Cyril VELTER
 * 
 *-------------------------------------------------------------------------
 */


#include "postgres.h"
#include <stdio.h>
#include <errno.h>
#include <OS.h>
#include "utils/elog.h"


/* Control of a semaphore pool. The pool is an area in which we stored all
the semIds of the pool. The first 4 bytes are the number of semaphore allocated
in the pool followed by SemIds */ 

int semctl(int semId,int semNum,int flag,union semun semun)
{
	int32* Address;
	area_info info; 

	/* Try to find the pool */
	if (get_area_info(semId,&info)!=B_OK)
	{
		/* pool is invalid (BeOS area id is invalid) */
		errno=EINVAL;
		return -1;
	}
	
	/* Get the pool address */
	Address=(int32*)info.address;
	
	
	/* semNum might be 0 */
	/* semun.array contain the sem initial values */
	
	/* Fix the count of all sem of the pool to semun.array */
	if (flag==SETALL)
	{
		long i;
		for (i=0;i<Address[0];i++)
		{
			int32 cnt;
			/* Get the current count */
			get_sem_count(Address[i+1],&cnt);

			/* Compute and set the new count (relative to the old one) */
			cnt-=semun.array[i];
			if (cnt > 0)
				while(acquire_sem_etc(Address[i+1],cnt,0,0)==B_INTERRUPTED);
			if (cnt < 0)
				release_sem_etc(Address[i+1],-cnt,0);
		}
		return 1;
	}
	
	/* Fix the count of one semaphore to semun.val */
	if (flag==SETVAL)
	{
		int32 cnt;
		/* Get the current count */
		get_sem_count(Address[semNum+1],&cnt);

		/* Compute and set the new count (relative to the old one) */
		cnt-=semun.val;
		if (cnt > 0)
			while(acquire_sem_etc(Address[semNum+1],cnt,0,0)==B_INTERRUPTED);
		if (cnt < 0)
			release_sem_etc(Address[semNum+1],-cnt,0);
		return 1;
	}
	
	/* Delete the pool */
	if (flag==IPC_RMID)
	{
		long i;

		thread_info ti;
		get_thread_info(find_thread(NULL),&ti);
		
		/* Loop over all semaphore to delete them */
		for (i=0;i<Address[0];i++)
		{
			/* Don't remember why I do that */
			set_sem_owner(Address[i+1],ti.team);
			
			/* Delete the semaphore */
			delete_sem(Address[i+1]);

			/* Reset to an invalid semId (in case other process try to get the infos from a cloned area */
			Address[i+1]=0;
		}
		
		/* Set the semaphore count to 0 */
		Address[0]=0;
		
		/* Delete the area (it might be cloned by other process. Let them live with it,
		in all cases semIds are 0 so if another process try to use it, it will fail */
		delete_area(semId);

		return 1;
	}
	
	/* Get the current semaphore count */
	if (flag==GETNCNT)
	{
		/* TO BE IMPLEMENTED */
		elog(ERROR,"beos : semctl error : GETNCNT not implemented");
		return 0;
	}
	
	/* Get the current semaphore count of the first semaphore in the pool */
	if (flag==GETVAL)
	{
		int32 cnt;
		get_sem_count(Address[semNum+1],&cnt);
		return cnt;
	}

	elog(ERROR,"beos : semctl error : unknown flag");

	return 0;
}

/* Find a pool id based on IPC key */
int semget(int semKey, int semNum, int flags)
{
	char Nom[50];
	area_id parea;
	void* Address;

	/* Name of the area to find */
	sprintf(Nom,"SYSV_IPC_SEM : %d",semKey);

	/* find area */
	parea=find_area(Nom);

	/* Test of area existance */
	if (parea!=B_NAME_NOT_FOUND)
	{
		/* Area exist and creation is requested, error */
		if ((flags&IPC_CREAT)&&(flags&IPC_EXCL))
		{
			errno=EEXIST;
			return -1;
		}
		
		/* Get an area clone (in case it's not in our address space) */
		/* TODO : a check of address space might be done to avoid duplicate areas in the same address space*/
		parea=clone_area(Nom,&Address,B_ANY_ADDRESS,B_READ_AREA | B_WRITE_AREA,parea);
		return parea;
	}
	else
	{
		/* Area does not  exist, but creation is requested, so create it */
		if (flags&IPC_CREAT)
		{
			int32* Address;
			void* Ad;
			long i;

			/* Limit to 500 semaphore in a pool */
			if (semNum>500)
			{
				errno=ENOSPC;
				return -1;
			}

			/* Create the shared memory area which will hold the pool */
			parea=create_area(Nom,&Ad,B_ANY_ADDRESS,4096,B_NO_LOCK,B_READ_AREA | B_WRITE_AREA);		
			if ((parea==B_BAD_VALUE)|| (parea==B_NO_MEMORY)||(parea==B_ERROR))
			{
				errno=ENOMEM;
				return -1;
			}
			
			/* fill up informations (sem number and sem ids) */
			Address=(int32*)Ad;
			Address[0]=semNum;
			for (i=1;i<=Address[0];i++)
			{
				/* Create the semaphores */
				Address[i]=create_sem(0,Nom);
				
				if ((Address[i]==B_BAD_VALUE)|| (Address[i]==B_NO_MEMORY)||(Address[i]==B_NO_MORE_SEMS))
				{
					errno=ENOMEM;
					return -1;
				}
			}

			return parea;
		}
		else
		{
			/* Area does not exist and no creation is requested */
			errno=ENOENT;
			return -1;
		}
	}
}

/* Acquire or release in the semaphore pool */
int semop(int semId, struct sembuf *sops, int nsops)
{
	int32* Address; /*Pool address*/
   	area_info info; 
	long i;

	/* Get the pool address (semId IS an area id) */
	get_area_info(semId,&info);
	Address=(int32*)info.address;
	
	/* Check the validity of semId (it should be an area id) */
	if ((semId==B_BAD_VALUE)||(semId==B_NO_MEMORY)||(semId==B_ERROR))
	{
		errno=EINVAL;
		return -1;
	}

	/* Perform acquire or release */
	for(i=0;i<nsops;i++)
	{
		/* For each sem in the pool, check the operation to perform */
		if (sops[i].sem_op < 0)
		{
			/* Try acuiring the semaphore till we are not inteerupted by a signal */
			while (acquire_sem_etc(Address[sops[i].sem_num+1],-sops[i].sem_op,0,0)==B_INTERRUPTED);
		}
		if (sops[i].sem_op > 0)
		{
			release_sem_etc(Address[sops[i].sem_num+1],sops[i].sem_op,0);
		}
	}
	return 0;
}