2004-02-23 06:31:08 +03:00
|
|
|
/*
|
2005-02-10 04:11:44 +03:00
|
|
|
* Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
2004-12-14 02:02:18 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
|
|
|
* Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2004-10-13 18:56:44 +04:00
|
|
|
/* Semaphore code */
|
|
|
|
|
2004-12-14 02:02:18 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <OS.h>
|
2004-03-16 05:53:41 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <sem.h>
|
2004-12-14 02:02:18 +03:00
|
|
|
#include <kernel.h>
|
2005-02-25 17:11:52 +03:00
|
|
|
#include <ksignal.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <smp.h>
|
|
|
|
#include <int.h>
|
|
|
|
#include <arch/int.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <thread.h>
|
2004-03-16 05:53:41 +03:00
|
|
|
#include <team.h>
|
2003-10-08 03:12:37 +04:00
|
|
|
#include <boot/kernel_args.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2004-12-14 02:02:18 +03:00
|
|
|
//#define TRACE_SEM
|
|
|
|
#ifdef TRACE_SEM
|
2002-09-30 07:43:32 +04:00
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
# define TRACE_BLOCK(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x) ;
|
|
|
|
# define TRACE_BLOCK(x) ;
|
|
|
|
#endif
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
struct sem_entry {
|
2002-10-26 20:13:36 +04:00
|
|
|
sem_id id;
|
2003-08-04 04:06:54 +04:00
|
|
|
spinlock lock; // protects only the id field when unused
|
|
|
|
union {
|
|
|
|
// when slot in use
|
|
|
|
struct {
|
|
|
|
int count;
|
2005-02-10 04:11:44 +03:00
|
|
|
struct thread_queue queue;
|
2003-08-04 04:06:54 +04:00
|
|
|
char *name;
|
|
|
|
team_id owner; // if set to -1, means owned by a port
|
|
|
|
} used;
|
|
|
|
|
|
|
|
// when slot unused
|
|
|
|
struct {
|
|
|
|
sem_id next_id;
|
|
|
|
struct sem_entry *next;
|
|
|
|
} unused;
|
|
|
|
} u;
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2003-08-04 04:06:54 +04:00
|
|
|
// Todo: Compute based on the amount of available memory.
|
2004-04-22 02:57:39 +04:00
|
|
|
static int32 sMaxSems = 4096;
|
2004-05-13 23:35:49 +04:00
|
|
|
static int32 sUsedSems = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
static struct sem_entry *sSems = NULL;
|
|
|
|
static bool sSemsActive = false;
|
|
|
|
static struct sem_entry *sFreeSemsHead = NULL;
|
|
|
|
static struct sem_entry *sFreeSemsTail = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-10-26 20:13:36 +04:00
|
|
|
static spinlock sem_spinlock = 0;
|
2002-07-21 02:52:04 +04:00
|
|
|
#define GRAB_SEM_LIST_LOCK() acquire_spinlock(&sem_spinlock)
|
|
|
|
#define RELEASE_SEM_LIST_LOCK() release_spinlock(&sem_spinlock)
|
|
|
|
#define GRAB_SEM_LOCK(s) acquire_spinlock(&(s).lock)
|
|
|
|
#define RELEASE_SEM_LOCK(s) release_spinlock(&(s).lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
static int remove_thread_from_sem(struct thread *thread, struct sem_entry *sem,
|
2004-10-13 19:19:08 +04:00
|
|
|
struct thread_queue *queue, status_t acquireStatus);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
struct sem_timeout_args {
|
|
|
|
thread_id blocked_thread;
|
|
|
|
sem_id blocked_sem_id;
|
|
|
|
int sem_count;
|
|
|
|
};
|
|
|
|
|
2002-08-05 09:23:23 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
dump_sem_list(int argc, char **argv)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2004-04-22 02:57:39 +04:00
|
|
|
for (i = 0; i < sMaxSems; i++) {
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[i].id >= 0)
|
|
|
|
dprintf("%p\tid: 0x%lx\t\tname: '%s'\n", &sSems[i], sSems[i].id,
|
|
|
|
sSems[i].u.used.name);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-07-18 02:07:37 +04:00
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:23:23 +04:00
|
|
|
|
|
|
|
static void
|
2002-09-30 07:43:32 +04:00
|
|
|
dump_sem(struct sem_entry *sem)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2003-08-04 04:06:54 +04:00
|
|
|
dprintf("SEM: %p\n", sem);
|
|
|
|
dprintf("id: %ld\n", sem->id);
|
|
|
|
if (sem->id >= 0) {
|
|
|
|
dprintf("name: '%s'\n", sem->u.used.name);
|
|
|
|
dprintf("owner: 0x%lx\n", sem->u.used.owner);
|
|
|
|
dprintf("count: 0x%x\n", sem->u.used.count);
|
2005-02-10 04:11:44 +03:00
|
|
|
dprintf("queue: head %p tail %p\n", sem->u.used.queue.head,
|
|
|
|
sem->u.used.queue.tail);
|
2003-08-04 04:06:54 +04:00
|
|
|
} else {
|
|
|
|
dprintf("next: %p\n", sem->u.unused.next);
|
|
|
|
dprintf("next_id: %ld\n", sem->u.unused.next_id);
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:23:23 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
dump_sem_info(int argc, char **argv)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if (argc < 2) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("sem: not enough arguments\n");
|
2002-07-18 02:07:37 +04:00
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the argument looks like a hex number, treat it as such
|
2002-07-21 02:52:04 +04:00
|
|
|
if (strlen(argv[1]) > 2 && argv[1][0] == '0' && argv[1][1] == 'x') {
|
2004-03-04 15:14:48 +03:00
|
|
|
unsigned long num = strtoul(argv[1], NULL, 16);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if (num > KERNEL_BASE && num <= (KERNEL_BASE + (KERNEL_SIZE - 1))) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// XXX semi-hack
|
2002-09-30 07:43:32 +04:00
|
|
|
dump_sem((struct sem_entry *)num);
|
2002-07-18 02:07:37 +04:00
|
|
|
return 0;
|
2002-09-30 07:43:32 +04:00
|
|
|
} else {
|
2004-04-22 02:57:39 +04:00
|
|
|
unsigned slot = num % sMaxSems;
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != (int)num) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("sem 0x%lx doesn't exist!\n", num);
|
2002-07-18 02:07:37 +04:00
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-11-08 16:57:40 +03:00
|
|
|
dump_sem(&sSems[slot]);
|
2002-07-18 02:07:37 +04:00
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// walk through the sem list, trying to match name
|
2004-04-22 02:57:39 +04:00
|
|
|
for (i = 0; i < sMaxSems; i++) {
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[i].u.used.name != NULL
|
|
|
|
&& strcmp(argv[1], sSems[i].u.used.name) == 0) {
|
|
|
|
dump_sem(&sSems[i]);
|
2002-09-30 07:43:32 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-08-05 09:23:23 +04:00
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2003-08-04 04:06:54 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
static inline void
|
|
|
|
clear_thread_queue(struct thread_queue *queue)
|
|
|
|
{
|
|
|
|
queue->head = queue->tail = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-13 19:19:08 +04:00
|
|
|
/** \brief Appends a semaphore slot to the free list.
|
|
|
|
*
|
|
|
|
* The semaphore list must be locked.
|
|
|
|
* The slot's id field is not changed. It should already be set to -1.
|
|
|
|
*
|
|
|
|
* \param slot The index of the semaphore slot.
|
|
|
|
* \param nextID The ID the slot will get when reused. If < 0 the \a slot
|
|
|
|
* is used.
|
|
|
|
*/
|
2003-08-04 04:06:54 +04:00
|
|
|
|
2004-10-13 19:19:08 +04:00
|
|
|
static void
|
2003-08-04 04:06:54 +04:00
|
|
|
free_sem_slot(int slot, sem_id nextID)
|
|
|
|
{
|
2004-11-08 16:57:40 +03:00
|
|
|
struct sem_entry *sem = sSems + slot;
|
2003-08-04 04:06:54 +04:00
|
|
|
// set next_id to the next possible value; for sanity check the current ID
|
|
|
|
if (nextID < 0)
|
|
|
|
sem->u.unused.next_id = slot;
|
|
|
|
else
|
|
|
|
sem->u.unused.next_id = nextID;
|
|
|
|
// append the entry to the list
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sFreeSemsTail)
|
|
|
|
sFreeSemsTail->u.unused.next = sem;
|
2003-08-04 04:06:54 +04:00
|
|
|
else
|
2004-11-08 16:57:40 +03:00
|
|
|
sFreeSemsHead = sem;
|
|
|
|
sFreeSemsTail = sem;
|
2003-08-04 04:06:54 +04:00
|
|
|
sem->u.unused.next = NULL;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:23:23 +04:00
|
|
|
|
2002-09-03 06:09:48 +04:00
|
|
|
status_t
|
2005-02-10 04:11:44 +03:00
|
|
|
sem_init(kernel_args *args)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-11-08 16:57:40 +03:00
|
|
|
area_id area;
|
2005-02-10 04:11:44 +03:00
|
|
|
int32 i;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("sem_init: entry\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// create and initialize semaphore table
|
2004-11-08 16:57:40 +03:00
|
|
|
area = create_area("sem_table", (void **)&sSems, B_ANY_KERNEL_ADDRESS,
|
2004-04-22 02:57:39 +04:00
|
|
|
sizeof(struct sem_entry) * sMaxSems, B_FULL_LOCK, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA);
|
2004-11-08 16:57:40 +03:00
|
|
|
if (area < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
panic("unable to allocate semaphore table!\n");
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
memset(sSems, 0, sizeof(struct sem_entry) * sMaxSems);
|
2004-04-22 02:57:39 +04:00
|
|
|
for (i = 0; i < sMaxSems; i++) {
|
2004-11-08 16:57:40 +03:00
|
|
|
sSems[i].id = -1;
|
2003-08-04 04:06:54 +04:00
|
|
|
free_sem_slot(i, i);
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// add debugger commands
|
2002-07-18 02:07:37 +04:00
|
|
|
add_debugger_command("sems", &dump_sem_list, "Dump a list of all active semaphores");
|
|
|
|
add_debugger_command("sem", &dump_sem_info, "Dump info about a particular semaphore");
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("sem_init: exit\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
sSemsActive = true;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
/** Creates a semaphore with the given parameters.
|
|
|
|
* Note, the team_id is not checked, it must be correct, or else
|
|
|
|
* that semaphore might not be deleted.
|
|
|
|
* This function is only available from within the kernel, and
|
|
|
|
* should not be made public - if possible, we should remove it
|
|
|
|
* completely (and have only create_sem() exported).
|
|
|
|
*/
|
|
|
|
|
|
|
|
sem_id
|
|
|
|
create_sem_etc(int32 count, const char *name, team_id owner)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2003-08-04 04:06:54 +04:00
|
|
|
struct sem_entry *sem = NULL;
|
2004-10-13 19:19:08 +04:00
|
|
|
cpu_status state;
|
|
|
|
sem_id id = B_NO_MORE_SEMS;
|
|
|
|
char *tempName;
|
|
|
|
size_t nameLength;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSemsActive == false || sUsedSems == sMaxSems)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2002-08-29 04:03:45 +04:00
|
|
|
if (name == NULL)
|
2002-09-11 16:23:56 +04:00
|
|
|
name = "unnamed semaphore";
|
2002-08-29 04:03:45 +04:00
|
|
|
|
2004-10-13 19:19:08 +04:00
|
|
|
nameLength = strlen(name) + 1;
|
|
|
|
nameLength = min(nameLength, B_OS_NAME_LENGTH);
|
|
|
|
tempName = (char *)malloc(nameLength);
|
|
|
|
if (tempName == NULL)
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_NO_MEMORY;
|
2004-10-13 19:19:08 +04:00
|
|
|
strlcpy(tempName, name, nameLength);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_SEM_LIST_LOCK();
|
|
|
|
|
2003-08-04 04:06:54 +04:00
|
|
|
// get the first slot from the free list
|
2004-11-08 16:57:40 +03:00
|
|
|
sem = sFreeSemsHead;
|
2003-08-04 04:06:54 +04:00
|
|
|
if (sem) {
|
|
|
|
// remove it from the free list
|
2004-11-08 16:57:40 +03:00
|
|
|
sFreeSemsHead = sem->u.unused.next;
|
|
|
|
if (!sFreeSemsHead)
|
|
|
|
sFreeSemsTail = NULL;
|
2005-02-10 04:11:44 +03:00
|
|
|
|
2003-08-04 04:06:54 +04:00
|
|
|
// init the slot
|
|
|
|
GRAB_SEM_LOCK(*sem);
|
|
|
|
sem->id = sem->u.unused.next_id;
|
|
|
|
sem->u.used.count = count;
|
2005-02-10 04:11:44 +03:00
|
|
|
clear_thread_queue(&sem->u.used.queue);
|
2004-10-13 19:19:08 +04:00
|
|
|
sem->u.used.name = tempName;
|
2003-08-04 04:06:54 +04:00
|
|
|
sem->u.used.owner = owner;
|
2004-10-13 19:19:08 +04:00
|
|
|
id = sem->id;
|
2004-05-13 23:35:49 +04:00
|
|
|
RELEASE_SEM_LOCK(*sem);
|
|
|
|
|
|
|
|
atomic_add(&sUsedSems, 1);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RELEASE_SEM_LIST_LOCK();
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-08-04 04:06:54 +04:00
|
|
|
if (!sem)
|
2004-10-13 19:19:08 +04:00
|
|
|
free(tempName);
|
2003-08-04 04:06:54 +04:00
|
|
|
|
2004-10-13 19:19:08 +04:00
|
|
|
return id;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
sem_id
|
|
|
|
create_sem(int32 count, const char *name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-08-03 04:41:27 +04:00
|
|
|
return create_sem_etc(count, name, team_get_kernel_team_id());
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
delete_sem(sem_id id)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-02-10 04:11:44 +03:00
|
|
|
struct thread_queue releaseQueue;
|
|
|
|
int32 releasedThreads;
|
|
|
|
struct thread *thread;
|
|
|
|
cpu_status state;
|
|
|
|
int32 slot;
|
|
|
|
char *name;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSemsActive == false)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (id < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
|
|
|
|
2004-04-22 02:57:39 +04:00
|
|
|
slot = id % sMaxSems;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != id) {
|
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("delete_sem: invalid sem_id %ld\n", id));
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
|
|
|
}
|
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
releasedThreads = 0;
|
|
|
|
clear_thread_queue(&releaseQueue);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// free any threads waiting for this semaphore
|
2005-02-10 04:11:44 +03:00
|
|
|
while ((thread = thread_dequeue(&sSems[slot].u.used.queue)) != NULL) {
|
|
|
|
thread->state = B_THREAD_READY;
|
|
|
|
thread->sem.acquire_status = B_BAD_SEM_ID;
|
|
|
|
thread->sem.count = 0;
|
|
|
|
thread_enqueue(thread, &releaseQueue);
|
|
|
|
releasedThreads++;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
sSems[slot].id = -1;
|
2005-02-10 04:11:44 +03:00
|
|
|
name = sSems[slot].u.used.name;
|
2004-11-08 16:57:40 +03:00
|
|
|
sSems[slot].u.used.name = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-08-04 04:06:54 +04:00
|
|
|
// append slot to the free list
|
|
|
|
GRAB_SEM_LIST_LOCK();
|
2004-04-22 02:57:39 +04:00
|
|
|
free_sem_slot(slot, id + sMaxSems);
|
2004-05-13 23:35:49 +04:00
|
|
|
atomic_add(&sUsedSems, -1);
|
2003-08-04 04:06:54 +04:00
|
|
|
RELEASE_SEM_LIST_LOCK();
|
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
if (releasedThreads > 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_THREAD_LOCK();
|
2005-02-10 04:11:44 +03:00
|
|
|
while ((thread = thread_dequeue(&releaseQueue)) != NULL) {
|
|
|
|
scheduler_enqueue_in_run_queue(thread);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2003-01-27 06:05:09 +03:00
|
|
|
scheduler_reschedule();
|
2002-07-09 16:24:59 +04:00
|
|
|
RELEASE_THREAD_LOCK();
|
|
|
|
}
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
free(name);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
/** Called from a timer handler. Wakes up a semaphore */
|
|
|
|
|
|
|
|
static int32
|
|
|
|
sem_timeout(timer *data)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-21 02:52:04 +04:00
|
|
|
struct sem_timeout_args *args = (struct sem_timeout_args *)data->entry.prev;
|
2005-02-10 04:11:44 +03:00
|
|
|
struct thread *thread;
|
2002-07-09 16:24:59 +04:00
|
|
|
int slot;
|
|
|
|
int state;
|
2005-02-10 04:11:44 +03:00
|
|
|
struct thread_queue wakeupQueue;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
thread = thread_get_thread_struct(args->blocked_thread);
|
|
|
|
if (thread == NULL)
|
2002-07-19 20:07:36 +04:00
|
|
|
return B_HANDLED_INTERRUPT;
|
2004-04-22 02:57:39 +04:00
|
|
|
slot = args->blocked_sem_id % sMaxSems;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-04 15:14:48 +03:00
|
|
|
TRACE(("sem_timeout: called on 0x%x sem %ld, tid %ld\n", data, args->blocked_sem_id, args->blocked_thread));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != args->blocked_sem_id) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// this thread was not waiting on this semaphore
|
2002-09-03 06:09:48 +04:00
|
|
|
panic("sem_timeout: thid %ld was trying to wait on sem %ld which doesn't exist!\n",
|
2002-07-09 16:24:59 +04:00
|
|
|
args->blocked_thread, args->blocked_sem_id);
|
|
|
|
}
|
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
clear_thread_queue(&wakeupQueue);
|
|
|
|
remove_thread_from_sem(thread, &sSems[slot], &wakeupQueue, B_TIMED_OUT);
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
GRAB_THREAD_LOCK();
|
|
|
|
// put the threads in the run q here to make sure we dont deadlock in sem_interrupt_thread
|
2005-02-10 04:11:44 +03:00
|
|
|
while ((thread = thread_dequeue(&wakeupQueue)) != NULL) {
|
|
|
|
scheduler_enqueue_in_run_queue(thread);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
RELEASE_THREAD_LOCK();
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-19 20:07:36 +04:00
|
|
|
return B_INVOKE_SCHEDULER;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
status_t
|
|
|
|
acquire_sem(sem_id id)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return acquire_sem_etc(id, 1, 0, 0);
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
status_t
|
|
|
|
acquire_sem_etc(sem_id id, int32 count, uint32 flags, bigtime_t timeout)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-04-22 02:57:39 +04:00
|
|
|
int slot = id % sMaxSems;
|
2002-07-09 16:24:59 +04:00
|
|
|
int state;
|
2002-09-30 07:43:32 +04:00
|
|
|
status_t status = B_OK;
|
2002-11-28 05:25:04 +03:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSemsActive == false)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2002-11-28 05:25:04 +03:00
|
|
|
|
|
|
|
if (!kernel_startup && !are_interrupts_enabled())
|
|
|
|
panic("acquire_sem_etc: called with interrupts disabled for sem %#lx\n", id);
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (id < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
2004-11-08 16:57:40 +03:00
|
|
|
if (count <= 0
|
|
|
|
|| (flags & (B_RELATIVE_TIMEOUT | B_ABSOLUTE_TIMEOUT)) == (B_RELATIVE_TIMEOUT | B_ABSOLUTE_TIMEOUT)) {
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_BAD_VALUE;
|
2004-11-08 16:57:40 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
2002-07-21 02:52:04 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != id) {
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("acquire_sem_etc: bad sem_id %ld\n", id));
|
|
|
|
status = B_BAD_SEM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
if (sSems[slot].u.used.count - count < 0 && (flags & B_RELATIVE_TIMEOUT) != 0
|
2003-08-04 04:06:54 +04:00
|
|
|
&& timeout <= 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// immediate timeout
|
2003-08-04 02:21:00 +04:00
|
|
|
status = B_WOULD_BLOCK;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if ((sSems[slot].u.used.count -= count) < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// we need to block
|
2005-02-10 04:11:44 +03:00
|
|
|
struct thread *thread = thread_get_current_thread();
|
2002-07-21 02:52:04 +04:00
|
|
|
timer timeout_timer; // stick it on the stack, since we may be blocking here
|
2002-07-09 16:24:59 +04:00
|
|
|
struct sem_timeout_args args;
|
|
|
|
|
2003-08-04 04:06:54 +04:00
|
|
|
TRACE_BLOCK(("acquire_sem_etc(id = %ld): block name = %s, thread = %p,"
|
2005-02-10 04:11:44 +03:00
|
|
|
" name = %s\n", id, sSems[slot].u.used.name, thread, thread->name));
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2002-10-23 21:31:10 +04:00
|
|
|
// do a quick check to see if the thread has any pending signals
|
2002-07-09 16:24:59 +04:00
|
|
|
// this should catch most of the cases where the thread had a signal
|
2005-02-25 17:11:52 +03:00
|
|
|
if (((flags & B_CAN_INTERRUPT) && thread->sig_pending)
|
|
|
|
|| ((flags & B_KILL_CAN_INTERRUPT)
|
|
|
|
&& (thread->sig_pending & KILL_SIGNALS))) {
|
2004-11-08 16:57:40 +03:00
|
|
|
sSems[slot].u.used.count += count;
|
2002-09-30 07:43:32 +04:00
|
|
|
status = B_INTERRUPTED;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2005-01-15 04:07:23 +03:00
|
|
|
if ((flags & (B_RELATIVE_TIMEOUT | B_ABSOLUTE_TIMEOUT)) == 0)
|
|
|
|
timeout = B_INFINITE_TIMEOUT;
|
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
thread->next_state = B_THREAD_WAITING;
|
|
|
|
thread->sem.flags = flags;
|
|
|
|
thread->sem.blocking = id;
|
|
|
|
thread->sem.acquire_count = count;
|
|
|
|
thread->sem.count = min(-sSems[slot].u.used.count, count);
|
2003-08-04 04:06:54 +04:00
|
|
|
// store the count we need to restore upon release
|
2005-02-10 04:11:44 +03:00
|
|
|
thread->sem.acquire_status = B_NO_ERROR;
|
|
|
|
thread_enqueue(thread, &sSems[slot].u.used.queue);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-01-15 04:07:23 +03:00
|
|
|
if (timeout != B_INFINITE_TIMEOUT) {
|
2004-03-04 15:14:48 +03:00
|
|
|
TRACE(("sem_acquire_etc: setting timeout sem for %Ld usecs, semid %d, tid %d\n",
|
2005-02-10 04:11:44 +03:00
|
|
|
timeout, id, thread->id));
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// set up an event to go off with the thread struct as the data
|
|
|
|
args.blocked_sem_id = id;
|
2005-02-10 04:11:44 +03:00
|
|
|
args.blocked_thread = thread->id;
|
2002-07-09 16:24:59 +04:00
|
|
|
args.sem_count = count;
|
2004-11-03 18:33:01 +03:00
|
|
|
|
|
|
|
// ToDo: another evil hack: pass the args into timer->entry.prev
|
2002-07-21 02:52:04 +04:00
|
|
|
timeout_timer.entry.prev = (qent *)&args;
|
|
|
|
add_timer(&timeout_timer, &sem_timeout, timeout,
|
|
|
|
flags & B_RELATIVE_TIMEOUT ?
|
|
|
|
B_ONE_SHOT_RELATIVE_TIMER : B_ONE_SHOT_ABSOLUTE_TIMER);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_THREAD_LOCK();
|
2002-10-23 21:31:10 +04:00
|
|
|
// check again to see if a signal is pending.
|
2002-07-09 16:24:59 +04:00
|
|
|
// it may have been delivered while setting up the sem, though it's pretty unlikely
|
2005-02-25 17:11:52 +03:00
|
|
|
if (((flags & B_CAN_INTERRUPT) && thread->sig_pending)
|
|
|
|
|| ((flags & B_KILL_CAN_INTERRUPT)
|
|
|
|
&& (thread->sig_pending & KILL_SIGNALS))) {
|
2005-02-10 04:11:44 +03:00
|
|
|
struct thread_queue wakeupQueue;
|
2002-07-09 16:24:59 +04:00
|
|
|
// ok, so a tiny race happened where a signal was delivered to this thread while
|
|
|
|
// it was setting up the sem. We can only be sure a signal wasn't delivered
|
|
|
|
// here, since the threadlock is held. The previous check would have found most
|
|
|
|
// instances, but there was a race, so we have to handle it. It'll be more messy...
|
2005-02-10 04:11:44 +03:00
|
|
|
clear_thread_queue(&wakeupQueue);
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
|
|
|
if (sSems[slot].id == id) {
|
2005-02-10 04:11:44 +03:00
|
|
|
remove_thread_from_sem(thread, &sSems[slot], &wakeupQueue, B_INTERRUPTED);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2005-02-10 04:11:44 +03:00
|
|
|
while ((thread = thread_dequeue(&wakeupQueue)) != NULL) {
|
|
|
|
scheduler_enqueue_in_run_queue(thread);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
// fall through and reschedule since another thread with a higher priority may have been woken up
|
|
|
|
}
|
2003-01-27 06:05:09 +03:00
|
|
|
scheduler_reschedule();
|
2002-07-09 16:24:59 +04:00
|
|
|
RELEASE_THREAD_LOCK();
|
|
|
|
|
2005-01-15 04:07:23 +03:00
|
|
|
if (timeout != B_INFINITE_TIMEOUT) {
|
2005-02-10 04:11:44 +03:00
|
|
|
if (thread->sem.acquire_status != B_TIMED_OUT) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// cancel the timer event, the sem may have been deleted or interrupted
|
|
|
|
// with the timer still active
|
2002-07-21 02:52:04 +04:00
|
|
|
cancel_timer(&timeout_timer);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-08-04 04:06:54 +04:00
|
|
|
TRACE_BLOCK(("acquire_sem_etc(id = %ld): exit block name = %s, "
|
2005-02-10 04:11:44 +03:00
|
|
|
"thread = %p (%s)\n", id, sSems[slot].u.used.name, thread,
|
|
|
|
thread->name));
|
|
|
|
return thread->sem.acquire_status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
release_sem(sem_id id)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return release_sem_etc(id, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
status_t
|
|
|
|
release_sem_etc(sem_id id, int32 count, uint32 flags)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-10-13 19:55:30 +04:00
|
|
|
struct thread_queue releaseQueue;
|
|
|
|
int32 slot = id % sMaxSems;
|
|
|
|
cpu_status state;
|
2002-09-30 07:43:32 +04:00
|
|
|
status_t status = B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSemsActive == false)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (id < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
2004-10-18 19:14:03 +04:00
|
|
|
if (count <= 0 && (flags & B_RELEASE_ALL) == 0)
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_BAD_VALUE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != id) {
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("sem_release_etc: invalid sem_id %ld\n", id));
|
|
|
|
status = B_BAD_SEM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear out a queue we will use to hold all of the threads that we will have to
|
|
|
|
// put back into the run list. This is done so the thread lock wont be held
|
|
|
|
// while this sems lock is held since the two locks are grabbed in the other
|
|
|
|
// order in sem_interrupt_thread.
|
2005-02-10 04:11:44 +03:00
|
|
|
clear_thread_queue(&releaseQueue);
|
2004-10-13 19:55:30 +04:00
|
|
|
|
2004-10-14 18:37:39 +04:00
|
|
|
if (flags & B_RELEASE_ALL) {
|
2004-11-08 16:57:40 +03:00
|
|
|
count = -sSems[slot].u.used.count;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-10-14 18:37:39 +04:00
|
|
|
// is there anything to do for us at all?
|
|
|
|
if (count == 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
while (count > 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
int delta = count;
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].u.used.count < 0) {
|
2005-02-10 04:11:44 +03:00
|
|
|
struct thread *thread = thread_lookat_queue(&sSems[slot].u.used.queue);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-10-13 19:55:30 +04:00
|
|
|
delta = min(count, thread->sem.count);
|
|
|
|
thread->sem.count -= delta;
|
|
|
|
if (thread->sem.count <= 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// release this thread
|
2005-02-10 04:11:44 +03:00
|
|
|
thread = thread_dequeue(&sSems[slot].u.used.queue);
|
2004-10-13 19:55:30 +04:00
|
|
|
thread_enqueue(thread, &releaseQueue);
|
|
|
|
thread->state = B_THREAD_READY;
|
|
|
|
thread->sem.count = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-10-13 19:55:30 +04:00
|
|
|
} else if (flags & B_RELEASE_IF_WAITING_ONLY)
|
|
|
|
break;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
sSems[slot].u.used.count += delta;
|
2002-07-09 16:24:59 +04:00
|
|
|
count -= delta;
|
|
|
|
}
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// pull off any items in the release queue and put them in the run queue
|
2004-10-13 19:55:30 +04:00
|
|
|
if (releaseQueue.head != NULL) {
|
|
|
|
struct thread *thread;
|
|
|
|
int32 priority;
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_THREAD_LOCK();
|
2004-10-13 19:55:30 +04:00
|
|
|
while ((thread = thread_dequeue(&releaseQueue)) != NULL) {
|
2002-08-01 00:05:24 +04:00
|
|
|
// temporarily place thread in a run queue with high priority to boost it up
|
2004-10-13 19:55:30 +04:00
|
|
|
priority = thread->priority;
|
|
|
|
thread->priority = thread->priority >= B_FIRST_REAL_TIME_PRIORITY ?
|
|
|
|
thread->priority : B_FIRST_REAL_TIME_PRIORITY;
|
|
|
|
scheduler_enqueue_in_run_queue(thread);
|
|
|
|
thread->priority = priority;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2003-01-27 06:05:09 +03:00
|
|
|
if ((flags & B_DO_NOT_RESCHEDULE) == 0)
|
|
|
|
scheduler_reschedule();
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
RELEASE_THREAD_LOCK();
|
|
|
|
}
|
|
|
|
goto outnolock;
|
|
|
|
|
|
|
|
err:
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
outnolock:
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
get_sem_count(sem_id id, int32 *thread_count)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int slot;
|
|
|
|
int state;
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSemsActive == false)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (id < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
|
|
|
if (thread_count == NULL)
|
2002-07-14 14:14:29 +04:00
|
|
|
return EINVAL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-04-22 02:57:39 +04:00
|
|
|
slot = id % sMaxSems;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != id) {
|
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("sem_get_count: invalid sem_id %ld\n", id));
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
|
|
|
}
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
*thread_count = sSems[slot].u.used.count;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2004-02-23 07:26:44 +03:00
|
|
|
/** Fills the thread_info structure with information from the specified
|
|
|
|
* thread.
|
|
|
|
* The thread lock must be held when called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
fill_sem_info(struct sem_entry *sem, sem_info *info, size_t size)
|
|
|
|
{
|
|
|
|
info->sem = sem->id;
|
|
|
|
info->team = sem->u.used.owner;
|
|
|
|
strlcpy(info->name, sem->u.used.name, sizeof(info->name));
|
|
|
|
info->count = sem->u.used.count;
|
2004-11-03 18:33:01 +03:00
|
|
|
|
|
|
|
// ToDo: not sure if this is the latest holder, or the next
|
|
|
|
// holder...
|
2005-02-10 04:11:44 +03:00
|
|
|
if (sem->u.used.queue.head != NULL)
|
|
|
|
info->latest_holder = sem->u.used.queue.head->id;
|
2004-11-03 18:33:01 +03:00
|
|
|
else
|
|
|
|
info->latest_holder = -1;
|
2004-02-23 07:26:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
/** The underscore is needed for binary compatibility with BeOS.
|
|
|
|
* OS.h contains the following macro:
|
|
|
|
* #define get_sem_info(sem, info) \
|
|
|
|
* _get_sem_info((sem), (info), sizeof(*(info)))
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
2004-02-23 07:26:44 +03:00
|
|
|
_get_sem_info(sem_id id, struct sem_info *info, size_t size)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-02-23 07:26:44 +03:00
|
|
|
status_t status = B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
int state;
|
|
|
|
int slot;
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (!sSemsActive)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (id < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
2004-02-23 07:26:44 +03:00
|
|
|
if (info == NULL || size != sizeof(sem_info))
|
|
|
|
return B_BAD_VALUE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-04-22 02:57:39 +04:00
|
|
|
slot = id % sMaxSems;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != id) {
|
2004-02-23 07:26:44 +03:00
|
|
|
status = B_BAD_SEM_ID;
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("get_sem_info: invalid sem_id %ld\n", id));
|
2004-02-23 07:26:44 +03:00
|
|
|
} else
|
2004-11-08 16:57:40 +03:00
|
|
|
fill_sem_info(&sSems[slot], info, size);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-02-23 07:26:44 +03:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
/** The underscore is needed for binary compatibility with BeOS.
|
|
|
|
* OS.h contains the following macro:
|
|
|
|
* #define get_next_sem_info(team, cookie, info) \
|
|
|
|
* _get_next_sem_info((team), (cookie), (info), sizeof(*(info)))
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
2004-02-23 07:26:44 +03:00
|
|
|
_get_next_sem_info(team_id team, int32 *_cookie, struct sem_info *info, size_t size)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int state;
|
|
|
|
int slot;
|
2003-08-04 02:21:00 +04:00
|
|
|
bool found = false;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (!sSemsActive)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2004-02-23 07:26:44 +03:00
|
|
|
if (_cookie == NULL || info == NULL || size != sizeof(sem_info))
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
if (team == B_CURRENT_TEAM)
|
|
|
|
team = team_get_current_team_id();
|
2004-11-08 16:57:40 +03:00
|
|
|
/* prevents sSems[].owner == -1 >= means owned by a port */
|
2004-02-23 07:26:44 +03:00
|
|
|
if (team < 0 || !team_is_valid(team))
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_BAD_TEAM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-02-23 07:26:44 +03:00
|
|
|
slot = *_cookie;
|
2004-04-22 02:57:39 +04:00
|
|
|
if (slot >= sMaxSems)
|
2004-02-23 07:26:44 +03:00
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_SEM_LIST_LOCK();
|
|
|
|
|
2004-04-22 02:57:39 +04:00
|
|
|
while (slot < sMaxSems) {
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != -1 && sSems[slot].u.used.owner == team) {
|
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
|
|
|
if (sSems[slot].id != -1 && sSems[slot].u.used.owner == team) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// found one!
|
2004-11-08 16:57:40 +03:00
|
|
|
fill_sem_info(&sSems[slot], info, size);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
slot++;
|
2003-08-04 02:21:00 +04:00
|
|
|
found = true;
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-09-30 07:43:32 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
slot++;
|
|
|
|
}
|
|
|
|
RELEASE_SEM_LIST_LOCK();
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-08-04 02:21:00 +04:00
|
|
|
if (!found)
|
|
|
|
return B_BAD_VALUE;
|
2004-02-23 07:26:44 +03:00
|
|
|
|
|
|
|
*_cookie = slot;
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
set_sem_owner(sem_id id, team_id team)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int state;
|
|
|
|
int slot;
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSemsActive == false)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (id < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
2004-02-23 06:31:08 +03:00
|
|
|
if (team < 0 || !team_is_valid(team))
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_BAD_TEAM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-04-22 02:57:39 +04:00
|
|
|
slot = id % sMaxSems;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[slot].id != id) {
|
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("set_sem_owner: invalid sem_id %ld\n", id));
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
|
|
|
}
|
|
|
|
|
2004-02-23 06:31:08 +03:00
|
|
|
// ToDo: this is a small race condition: the team ID could already
|
2002-09-30 07:48:59 +04:00
|
|
|
// be invalid at this point - we would lose one semaphore slot in
|
|
|
|
// this case!
|
2004-02-23 06:31:08 +03:00
|
|
|
// The only safe way to do this is to prevent either team (the new
|
|
|
|
// or the old owner) from dying until we leave the spinlock.
|
2004-11-08 16:57:40 +03:00
|
|
|
sSems[slot].u.used.owner = team;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
/** Wake up a thread that's blocked on a semaphore
|
|
|
|
* this function must be entered with interrupts disabled and THREADLOCK held
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
2005-02-10 04:11:44 +03:00
|
|
|
sem_interrupt_thread(struct thread *thread)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-02-10 04:11:44 +03:00
|
|
|
struct thread_queue wakeupQueue;
|
|
|
|
int32 slot;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-02-23 06:31:08 +03:00
|
|
|
TRACE(("sem_interrupt_thread: called on thread %p (%d), blocked on sem 0x%x\n",
|
2005-02-10 04:11:44 +03:00
|
|
|
thread, thread->id, thread->sem.blocking));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
if (thread->state != B_THREAD_WAITING || thread->sem.blocking < 0)
|
2004-02-23 06:31:08 +03:00
|
|
|
return B_BAD_VALUE;
|
2005-02-25 17:11:52 +03:00
|
|
|
if ((thread->sem.flags & B_CAN_INTERRUPT) == 0
|
|
|
|
&& ((thread->sem.flags & B_KILL_CAN_INTERRUPT) == 0
|
|
|
|
|| (thread->sig_pending & KILL_SIGNALS) == 0)) {
|
2004-02-23 06:31:08 +03:00
|
|
|
return B_NOT_ALLOWED;
|
2005-02-25 17:11:52 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
slot = thread->sem.blocking % sMaxSems;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
GRAB_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
if (sSems[slot].id != thread->sem.blocking) {
|
|
|
|
panic("sem_interrupt_thread: thread 0x%lx sez it's blocking on sem 0x%lx, but that sem doesn't exist!\n", thread->id, thread->sem.blocking);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
clear_thread_queue(&wakeupQueue);
|
|
|
|
if (remove_thread_from_sem(thread, &sSems[slot], &wakeupQueue, B_INTERRUPTED) != B_OK) {
|
|
|
|
panic("sem_interrupt_thread: thread 0x%lx not found in sem 0x%lx's wait queue\n",
|
|
|
|
thread->id, thread->sem.blocking);
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
RELEASE_SEM_LOCK(sSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
while ((thread = thread_dequeue(&wakeupQueue)) != NULL) {
|
|
|
|
scheduler_enqueue_in_run_queue(thread);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
/** Forcibly removes a thread from a semaphores wait queue. May have to wake up
|
|
|
|
* other threads in the process. All threads that need to be woken up are added
|
|
|
|
* to the passed in thread_queue.
|
|
|
|
* Must be called with semaphore lock held.
|
2002-09-30 07:43:32 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2005-02-10 04:11:44 +03:00
|
|
|
remove_thread_from_sem(struct thread *thread, struct sem_entry *sem,
|
|
|
|
struct thread_queue *queue, status_t acquireStatus)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
// remove the thread from the queue and place it in the supplied queue
|
2005-02-10 04:11:44 +03:00
|
|
|
if (thread_dequeue_id(&sem->u.used.queue, thread->id) != thread)
|
2004-02-23 06:31:08 +03:00
|
|
|
return B_ENTRY_NOT_FOUND;
|
|
|
|
|
2005-02-10 04:11:44 +03:00
|
|
|
sem->u.used.count += thread->sem.acquire_count;
|
|
|
|
thread->state = thread->next_state = B_THREAD_READY;
|
|
|
|
thread->sem.acquire_status = acquireStatus;
|
|
|
|
thread_enqueue(thread, queue);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// now see if more threads need to be woken up
|
2003-08-04 04:06:54 +04:00
|
|
|
while (sem->u.used.count > 0
|
2005-02-10 04:11:44 +03:00
|
|
|
&& thread_lookat_queue(&sem->u.used.queue) != NULL) {
|
|
|
|
int32 delta = min(thread->sem.count, sem->u.used.count);
|
|
|
|
|
|
|
|
thread->sem.count -= delta;
|
|
|
|
if (thread->sem.count <= 0) {
|
|
|
|
thread = thread_dequeue(&sem->u.used.queue);
|
|
|
|
thread->state = thread->next_state = B_THREAD_READY;
|
|
|
|
thread_enqueue(thread, queue);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2003-08-04 04:06:54 +04:00
|
|
|
sem->u.used.count -= delta;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2005-02-10 04:11:44 +03:00
|
|
|
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
/** this function cycles through the sem table, deleting all the sems that are owned by
|
|
|
|
* the passed team_id
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_delete_owned_sems(team_id owner)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int state;
|
|
|
|
int i;
|
|
|
|
int count = 0;
|
|
|
|
|
2004-11-08 16:57:40 +03:00
|
|
|
// ToDo: that looks horribly inefficient - maybe it would be better
|
|
|
|
// to have them in a list in the team
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
if (owner < 0)
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_BAD_TEAM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_SEM_LIST_LOCK();
|
|
|
|
|
2004-04-22 02:57:39 +04:00
|
|
|
for (i = 0; i < sMaxSems; i++) {
|
2004-11-08 16:57:40 +03:00
|
|
|
if (sSems[i].id != -1 && sSems[i].u.used.owner == owner) {
|
|
|
|
sem_id id = sSems[i].id;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
RELEASE_SEM_LIST_LOCK();
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-10-26 05:11:15 +04:00
|
|
|
delete_sem(id);
|
2002-07-09 16:24:59 +04:00
|
|
|
count++;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_SEM_LIST_LOCK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RELEASE_SEM_LIST_LOCK();
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2004-04-22 02:57:39 +04:00
|
|
|
int32
|
|
|
|
sem_max_sems(void)
|
|
|
|
{
|
|
|
|
return sMaxSems;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32
|
|
|
|
sem_used_sems(void)
|
|
|
|
{
|
2004-05-13 23:35:49 +04:00
|
|
|
return sUsedSems;
|
2004-04-22 02:57:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-23 06:54:00 +03:00
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
sem_id
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_create_sem(int32 count, const char *userName)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-02-23 06:54:00 +03:00
|
|
|
char name[B_OS_NAME_LENGTH];
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-02-23 06:54:00 +03:00
|
|
|
if (userName == NULL)
|
|
|
|
return create_sem_etc(count, NULL, team_get_current_team_id());
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-02-23 06:54:00 +03:00
|
|
|
if (!IS_USER_ADDRESS(userName)
|
|
|
|
|| user_strlcpy(name, userName, B_OS_NAME_LENGTH) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2004-02-23 06:54:00 +03:00
|
|
|
return create_sem_etc(count, name, team_get_current_team_id());
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_delete_sem(sem_id id)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return delete_sem(id);
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_acquire_sem(sem_id id)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-29 00:51:47 +04:00
|
|
|
return _user_acquire_sem_etc(id, 1, 0, 0);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_acquire_sem_etc(sem_id id, int32 count, uint32 flags, bigtime_t timeout)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-02-23 06:54:00 +03:00
|
|
|
return acquire_sem_etc(id, count, flags | B_CAN_INTERRUPT, timeout);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_release_sem(sem_id id)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return release_sem_etc(id, 1, 0);
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_release_sem_etc(sem_id id, int32 count, uint32 flags)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return release_sem_etc(id, count, flags);
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_get_sem_count(sem_id id, int32 *userCount)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-02-23 06:54:00 +03:00
|
|
|
status_t status;
|
|
|
|
int32 count;
|
|
|
|
|
|
|
|
if (userCount == NULL || !IS_USER_ADDRESS(userCount))
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
status = get_sem_count(id, &count);
|
|
|
|
if (status == B_OK && user_memcpy(userCount, &count, sizeof(int32)) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_get_sem_info(sem_id id, struct sem_info *userInfo, size_t size)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct sem_info info;
|
2004-02-23 06:54:00 +03:00
|
|
|
status_t status;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-02-23 06:54:00 +03:00
|
|
|
if (userInfo == NULL || !IS_USER_ADDRESS(userInfo))
|
|
|
|
return B_BAD_ADDRESS;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-02-23 06:54:00 +03:00
|
|
|
status = _get_sem_info(id, &info, size);
|
|
|
|
if (status == B_OK && user_memcpy(userInfo, &info, size) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2004-02-23 06:54:00 +03:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_get_next_sem_info(team_id team, int32 *userCookie, struct sem_info *userInfo,
|
2004-02-23 06:54:00 +03:00
|
|
|
size_t size)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct sem_info info;
|
2002-09-03 06:09:48 +04:00
|
|
|
int32 cookie;
|
2004-02-23 06:54:00 +03:00
|
|
|
status_t status;
|
|
|
|
|
|
|
|
if (userCookie == NULL || userInfo == NULL
|
|
|
|
|| !IS_USER_ADDRESS(userCookie) || !IS_USER_ADDRESS(userInfo)
|
|
|
|
|| user_memcpy(&cookie, userCookie, sizeof(int32)) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
status = _get_next_sem_info(team, &cookie, &info, size);
|
|
|
|
|
|
|
|
if (status == B_OK) {
|
|
|
|
if (user_memcpy(userInfo, &info, size) < B_OK
|
|
|
|
|| user_memcpy(userCookie, &cookie, sizeof(int32)) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_set_sem_owner(sem_id id, team_id team)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-02-23 06:54:00 +03:00
|
|
|
return set_sem_owner(id, team);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|