2002-07-09 16:24:59 +04:00
|
|
|
/* Semaphore code. Lots of "todo" items*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <OS.h>
|
|
|
|
#include <sem.h>
|
|
|
|
#include <smp.h>
|
|
|
|
#include <int.h>
|
|
|
|
#include <arch/int.h>
|
|
|
|
#include <timer.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <memheap.h>
|
|
|
|
#include <thread.h>
|
|
|
|
#include <Errors.h>
|
2002-07-12 02:21:56 +04:00
|
|
|
#include <kerrors.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#include <stage2.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
#define TRACE_SEM 0
|
|
|
|
#if TRACE_SEM
|
|
|
|
# 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-09-30 07:43:32 +04:00
|
|
|
sem_id id;
|
|
|
|
int count;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct thread_queue q;
|
2002-09-30 07:43:32 +04:00
|
|
|
char *name;
|
|
|
|
int lock;
|
|
|
|
team_id owner; // if set to -1, means owned by a port
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_SEMS 4096
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
static struct sem_entry *gSems = NULL;
|
|
|
|
static region_id gSemRegion = 0;
|
|
|
|
static bool gSemsActive = false;
|
|
|
|
static sem_id gNextSemID = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
static int 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
|
|
|
|
|
|
|
// used in functions that may put a bunch of threads in the run q at once
|
|
|
|
#define READY_THREAD_CACHE_SIZE 16
|
|
|
|
|
|
|
|
static int remove_thread_from_sem(struct thread *t, struct sem_entry *sem, struct thread_queue *queue, int sem_errcode);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
for (i = 0; i < MAX_SEMS; i++) {
|
|
|
|
if (gSems[i].id >= 0)
|
|
|
|
dprintf("%p\tid: 0x%lx\t\tname: '%s'\n", &gSems[i], gSems[i].id, gSems[i].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
|
|
|
{
|
|
|
|
dprintf("SEM: %p\n", sem);
|
|
|
|
dprintf("name: '%s'\n", sem->name);
|
2002-09-03 06:09:48 +04:00
|
|
|
dprintf("owner: 0x%lx\n", sem->owner);
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("count: 0x%x\n", sem->count);
|
|
|
|
dprintf("queue: head %p tail %p\n", sem->q.head, sem->q.tail);
|
|
|
|
}
|
|
|
|
|
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') {
|
2002-07-09 16:24:59 +04:00
|
|
|
unsigned long num = atoul(argv[1]);
|
|
|
|
|
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 {
|
2002-07-09 16:24:59 +04:00
|
|
|
unsigned slot = num % MAX_SEMS;
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[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
|
|
|
}
|
2002-09-30 07:43:32 +04:00
|
|
|
dump_sem(&gSems[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
|
2002-09-30 07:43:32 +04:00
|
|
|
for (i = 0; i < MAX_SEMS; i++) {
|
|
|
|
if (gSems[i].name != NULL
|
|
|
|
&& strcmp(argv[1], gSems[i].name) == 0) {
|
|
|
|
dump_sem(&gSems[i]);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-08-05 09:23:23 +04:00
|
|
|
|
2002-09-03 06:09:48 +04:00
|
|
|
status_t
|
2002-08-05 09:23:23 +04:00
|
|
|
sem_init(kernel_args *ka)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
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
|
2002-09-30 07:43:32 +04:00
|
|
|
gSemRegion = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "sem_table",
|
|
|
|
(void **)&gSems, REGION_ADDR_ANY_ADDRESS, sizeof(struct sem_entry) * MAX_SEMS,
|
|
|
|
REGION_WIRING_WIRED, LOCK_RW | LOCK_KERNEL);
|
|
|
|
if (gSemRegion < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
panic("unable to allocate semaphore table!\n");
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
memset(gSems, 0, sizeof(struct sem_entry) * MAX_SEMS);
|
|
|
|
for (i = 0; i < MAX_SEMS; i++)
|
|
|
|
gSems[i].id = -1;
|
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
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
gSemsActive = 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
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int state;
|
|
|
|
sem_id retval = B_NO_MORE_SEMS;
|
|
|
|
char *temp_name;
|
2002-08-29 04:03:45 +04:00
|
|
|
int name_len;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSemsActive == false)
|
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
|
|
|
|
2002-09-11 16:23:56 +04:00
|
|
|
name_len = strlen(name) + 1;
|
|
|
|
name_len = min(name_len, SYS_MAX_OS_NAME_LEN);
|
2002-08-29 04:03:45 +04:00
|
|
|
temp_name = (char *)kmalloc(name_len);
|
|
|
|
if (temp_name == NULL)
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_NO_MEMORY;
|
2002-08-29 04:03:45 +04:00
|
|
|
strlcpy(temp_name, name, name_len);
|
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();
|
|
|
|
|
|
|
|
// find the first empty spot
|
2002-09-30 07:43:32 +04:00
|
|
|
for (i = 0; i < MAX_SEMS; i++) {
|
|
|
|
if (gSems[i].id == -1) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// make the sem id be a multiple of the slot it's in
|
2002-09-30 07:43:32 +04:00
|
|
|
if (i >= gNextSemID % MAX_SEMS)
|
|
|
|
gNextSemID += i - gNextSemID % MAX_SEMS;
|
2002-07-21 02:52:04 +04:00
|
|
|
else
|
2002-09-30 07:43:32 +04:00
|
|
|
gNextSemID += MAX_SEMS - (gNextSemID % MAX_SEMS - i);
|
|
|
|
gSems[i].id = gNextSemID++;
|
|
|
|
|
|
|
|
// Set the owner while the sem list lock is hold, or else it
|
|
|
|
// might get lost if we have a sem_delete_owned_sems() running
|
|
|
|
// (although this should be impossible (if create_sem_etc() is
|
|
|
|
// just properly, I just feel better with it).
|
|
|
|
gSems[i].owner = owner;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
gSems[i].lock = 0;
|
|
|
|
GRAB_SEM_LOCK(gSems[i]);
|
2002-07-09 16:24:59 +04:00
|
|
|
RELEASE_SEM_LIST_LOCK();
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
gSems[i].q.tail = NULL;
|
|
|
|
gSems[i].q.head = NULL;
|
|
|
|
gSems[i].count = count;
|
|
|
|
gSems[i].name = temp_name;
|
|
|
|
retval = gSems[i].id;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[i]);
|
2002-07-09 16:24:59 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RELEASE_SEM_LIST_LOCK();
|
|
|
|
kfree(temp_name);
|
|
|
|
|
|
|
|
out:
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return delete_sem_etc(id, 0);
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
delete_sem_etc(sem_id id, status_t return_code)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int slot;
|
|
|
|
int state;
|
|
|
|
struct thread *t;
|
|
|
|
int released_threads;
|
|
|
|
char *old_name;
|
|
|
|
struct thread_queue release_queue;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSemsActive == 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;
|
|
|
|
|
|
|
|
slot = id % MAX_SEMS;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].id != id) {
|
|
|
|
RELEASE_SEM_LOCK(gSems[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;
|
|
|
|
}
|
|
|
|
|
|
|
|
released_threads = 0;
|
|
|
|
release_queue.head = release_queue.tail = NULL;
|
|
|
|
|
|
|
|
// free any threads waiting for this semaphore
|
2002-09-30 07:43:32 +04:00
|
|
|
while ((t = thread_dequeue(&gSems[slot].q)) != NULL) {
|
2002-08-05 00:10:06 +04:00
|
|
|
t->state = B_THREAD_READY;
|
2002-07-09 16:24:59 +04:00
|
|
|
t->sem_errcode = B_BAD_SEM_ID;
|
|
|
|
t->sem_deleted_retcode = return_code;
|
|
|
|
t->sem_count = 0;
|
|
|
|
thread_enqueue(t, &release_queue);
|
|
|
|
released_threads++;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
gSems[slot].id = -1;
|
|
|
|
old_name = gSems[slot].name;
|
|
|
|
gSems[slot].name = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if (released_threads > 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_THREAD_LOCK();
|
2002-07-21 02:52:04 +04:00
|
|
|
while ((t = thread_dequeue(&release_queue)) != NULL) {
|
2002-07-09 16:24:59 +04:00
|
|
|
thread_enqueue_run_q(t);
|
|
|
|
}
|
2002-08-04 03:39:50 +04:00
|
|
|
resched();
|
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
|
|
|
|
|
|
|
kfree(old_name);
|
|
|
|
|
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;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct thread *t;
|
|
|
|
int slot;
|
|
|
|
int state;
|
|
|
|
struct thread_queue wakeup_queue;
|
|
|
|
|
|
|
|
t = thread_get_thread_struct(args->blocked_thread);
|
2002-07-21 02:52:04 +04:00
|
|
|
if (t == NULL)
|
2002-07-19 20:07:36 +04:00
|
|
|
return B_HANDLED_INTERRUPT;
|
2002-07-09 16:24:59 +04:00
|
|
|
slot = args->blocked_sem_id % MAX_SEMS;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("sem_timeout: called on 0x%x sem %d, tid %d\n", to, to->sem_id, to->thread_id));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[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);
|
|
|
|
}
|
|
|
|
|
|
|
|
wakeup_queue.head = wakeup_queue.tail = NULL;
|
2002-09-30 07:43:32 +04:00
|
|
|
remove_thread_from_sem(t, &gSems[slot], &wakeup_queue, B_TIMED_OUT);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[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
|
2002-07-21 02:52:04 +04:00
|
|
|
while ((t = thread_dequeue(&wakeup_queue)) != NULL) {
|
2002-07-09 16:24:59 +04:00
|
|
|
thread_enqueue_run_q(t);
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
int slot = id % MAX_SEMS;
|
|
|
|
int state;
|
2002-09-30 07:43:32 +04:00
|
|
|
status_t status = B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSemsActive == false)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
2002-09-30 07:43:32 +04:00
|
|
|
if (id < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (count <= 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();
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
2002-07-21 02:52:04 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].id != id) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].count - count < 0 && (flags & B_TIMEOUT) != 0 && timeout <= 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// immediate timeout
|
2002-09-30 07:43:32 +04:00
|
|
|
status = B_TIMED_OUT;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if ((gSems[slot].count -= count) < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// we need to block
|
|
|
|
struct thread *t = 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;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE_BLOCK(("acquire_sem_etc(id = %ld): block name = %s, thread = %p, name = %s\n", id, gSems[slot].name, t, t->name));
|
|
|
|
|
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
|
2002-10-23 21:31:10 +04:00
|
|
|
if ((flags & B_CAN_INTERRUPT) && t->sig_pending) {
|
2002-09-30 07:43:32 +04:00
|
|
|
gSems[slot].count += count;
|
|
|
|
status = B_INTERRUPTED;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:10:06 +04:00
|
|
|
t->next_state = B_THREAD_WAITING;
|
2002-07-09 16:24:59 +04:00
|
|
|
t->sem_flags = flags;
|
|
|
|
t->sem_blocking = id;
|
|
|
|
t->sem_acquire_count = count;
|
2002-09-30 07:43:32 +04:00
|
|
|
t->sem_count = min(-gSems[slot].count, count); // store the count we need to restore upon release
|
2002-07-09 16:24:59 +04:00
|
|
|
t->sem_deleted_retcode = 0;
|
|
|
|
t->sem_errcode = B_NO_ERROR;
|
2002-09-30 07:43:32 +04:00
|
|
|
thread_enqueue(t, &gSems[slot].q);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if ((flags & (B_TIMEOUT | B_ABSOLUTE_TIMEOUT)) != 0) {
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("sem_acquire_etc: setting timeout sem for %d %d usecs, semid %d, tid %d\n",
|
|
|
|
timeout, sem_id, t->id));
|
|
|
|
|
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;
|
|
|
|
args.blocked_thread = t->id;
|
|
|
|
args.sem_count = count;
|
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
// another evil hack: pass the args into timer->entry.prev
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[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
|
2002-10-23 21:31:10 +04:00
|
|
|
if ((flags & B_CAN_INTERRUPT) && t->sig_pending) {
|
2002-07-09 16:24:59 +04:00
|
|
|
struct thread_queue wakeup_queue;
|
|
|
|
// 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...
|
|
|
|
wakeup_queue.head = wakeup_queue.tail = NULL;
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
|
|
|
if (gSems[slot].id == id) {
|
|
|
|
remove_thread_from_sem(t, &gSems[slot], &wakeup_queue, EINTR);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[slot]);
|
2002-07-21 02:52:04 +04:00
|
|
|
while ((t = thread_dequeue(&wakeup_queue)) != NULL) {
|
2002-07-09 16:24:59 +04:00
|
|
|
thread_enqueue_run_q(t);
|
|
|
|
}
|
|
|
|
// fall through and reschedule since another thread with a higher priority may have been woken up
|
|
|
|
}
|
2002-08-04 03:39:50 +04:00
|
|
|
resched();
|
2002-07-09 16:24:59 +04:00
|
|
|
RELEASE_THREAD_LOCK();
|
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if ((flags & (B_TIMEOUT | B_ABSOLUTE_TIMEOUT)) != 0) {
|
|
|
|
if (t->sem_errcode != 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
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE_BLOCK(("acquire_sem_etc(id = %ld): exit block name = %s, thread = %p (%s)\n", id, gSems[slot].name, t, t->name));
|
2002-07-09 16:24:59 +04:00
|
|
|
return t->sem_errcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[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
|
|
|
{
|
|
|
|
int slot = id % MAX_SEMS;
|
|
|
|
int state;
|
|
|
|
int released_threads = 0;
|
|
|
|
struct thread_queue release_queue;
|
2002-09-30 07:43:32 +04:00
|
|
|
status_t status = B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSemsActive == 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;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (count <= 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();
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].id != id) {
|
|
|
|
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.
|
|
|
|
release_queue.head = release_queue.tail = NULL;
|
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
while (count > 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
int delta = count;
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].count < 0) {
|
|
|
|
struct thread *t = thread_lookat_queue(&gSems[slot].q);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
delta = min(count, t->sem_count);
|
|
|
|
t->sem_count -= delta;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (t->sem_count <= 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// release this thread
|
2002-09-30 07:43:32 +04:00
|
|
|
t = thread_dequeue(&gSems[slot].q);
|
2002-07-09 16:24:59 +04:00
|
|
|
thread_enqueue(t, &release_queue);
|
2002-08-05 00:10:06 +04:00
|
|
|
t->state = B_THREAD_READY;
|
2002-07-09 16:24:59 +04:00
|
|
|
released_threads++;
|
|
|
|
t->sem_count = 0;
|
|
|
|
t->sem_deleted_retcode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
gSems[slot].count += delta;
|
2002-07-09 16:24:59 +04:00
|
|
|
count -= delta;
|
|
|
|
}
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// pull off any items in the release queue and put them in the run queue
|
2002-07-21 02:52:04 +04:00
|
|
|
if (released_threads > 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
struct thread *t;
|
2002-08-01 00:05:24 +04:00
|
|
|
int priority;
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_THREAD_LOCK();
|
2002-07-21 02:52:04 +04:00
|
|
|
while ((t = thread_dequeue(&release_queue)) != NULL) {
|
2002-08-01 00:05:24 +04:00
|
|
|
// temporarily place thread in a run queue with high priority to boost it up
|
|
|
|
priority = t->priority;
|
2002-08-05 00:10:06 +04:00
|
|
|
t->priority = t->priority >= B_FIRST_REAL_TIME_PRIORITY ? t->priority : B_FIRST_REAL_TIME_PRIORITY;
|
2002-07-09 16:24:59 +04:00
|
|
|
thread_enqueue_run_q(t);
|
2002-08-01 00:05:24 +04:00
|
|
|
t->priority = priority;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-07-21 02:52:04 +04:00
|
|
|
if ((flags & B_DO_NOT_RESCHEDULE) == 0) {
|
2002-08-04 03:39:50 +04:00
|
|
|
resched();
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
RELEASE_THREAD_LOCK();
|
|
|
|
}
|
|
|
|
goto outnolock;
|
|
|
|
|
|
|
|
err:
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[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;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSemsActive == 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
|
|
|
|
|
|
|
slot = id % MAX_SEMS;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].id != id) {
|
|
|
|
RELEASE_SEM_LOCK(gSems[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;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
*thread_count = gSems[slot].count;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[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
|
|
|
|
|
|
|
/** 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
|
|
|
|
_get_sem_info(sem_id id, struct sem_info *info, size_t sz)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int state;
|
|
|
|
int slot;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSemsActive == 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 (info == NULL)
|
2002-07-14 14:14:29 +04:00
|
|
|
return EINVAL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
slot = id % MAX_SEMS;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].id != id) {
|
|
|
|
RELEASE_SEM_LOCK(gSems[slot]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("get_sem_info: invalid sem_id %ld\n", id));
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_BAD_SEM_ID;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
info->sem = gSems[slot].id;
|
|
|
|
info->team = gSems[slot].owner;
|
|
|
|
strncpy(info->name, gSems[slot].name, SYS_MAX_OS_NAME_LEN-1);
|
|
|
|
info->count = gSems[slot].count;
|
|
|
|
info->latest_holder = gSems[slot].q.head->id; // XXX not sure if this is correct
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[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
|
|
|
|
|
|
|
/** 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
|
|
|
|
_get_next_sem_info(team_id team, int32 *cookie, struct sem_info *info, size_t sz)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int state;
|
|
|
|
int slot;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSemsActive == false)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_MORE_SEMS;
|
|
|
|
if (cookie == NULL)
|
2002-07-14 14:10:22 +04:00
|
|
|
return EINVAL;
|
2002-09-30 07:43:32 +04:00
|
|
|
/* prevents gSems[].owner == -1 >= means owned by a port */
|
2002-08-03 04:41:27 +04:00
|
|
|
if (team < 0)
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_BAD_TEAM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-10-25 15:04:11 +04:00
|
|
|
if (*cookie == 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// return first found
|
|
|
|
slot = 0;
|
2002-07-21 02:52:04 +04:00
|
|
|
}
|
|
|
|
else {
|
2002-07-09 16:24:59 +04:00
|
|
|
// start at index cookie, but check cookie against MAX_PORTS
|
|
|
|
slot = *cookie;
|
|
|
|
if (slot >= MAX_SEMS)
|
|
|
|
return B_BAD_SEM_ID;
|
|
|
|
}
|
|
|
|
// spinlock
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
GRAB_SEM_LIST_LOCK();
|
|
|
|
|
|
|
|
while (slot < MAX_SEMS) {
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].id != -1 && gSems[slot].owner == team) {
|
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
|
|
|
if (gSems[slot].id != -1 && gSems[slot].owner == team) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// found one!
|
2002-09-30 07:43:32 +04:00
|
|
|
info->sem = gSems[slot].id;
|
|
|
|
info->team = gSems[slot].owner;
|
|
|
|
strncpy(info->name, gSems[slot].name, SYS_MAX_OS_NAME_LEN-1);
|
|
|
|
info->count = gSems[slot].count;
|
|
|
|
info->latest_holder = gSems[slot].q.head->id; // XXX not sure if this is the latest holder, or the next holder...
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
slot++;
|
|
|
|
break;
|
|
|
|
}
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[slot]);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
if (slot == MAX_SEMS)
|
2002-07-14 14:10:22 +04:00
|
|
|
return B_BAD_SEM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
*cookie = slot;
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSemsActive == 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;
|
2002-08-03 04:41:27 +04:00
|
|
|
if (team < 0)
|
2002-09-30 07:43:32 +04:00
|
|
|
return B_BAD_TEAM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
// check if the team ID is valid
|
|
|
|
if (team_get_team_struct(team) == NULL)
|
|
|
|
return B_BAD_TEAM_ID;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
slot = id % MAX_SEMS;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].id != id) {
|
|
|
|
RELEASE_SEM_LOCK(gSems[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;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:48:59 +04:00
|
|
|
// Todo: this is a small race condition: the team ID could already
|
|
|
|
// be invalid at this point - we would lose one semaphore slot in
|
|
|
|
// this case!
|
2002-09-30 07:43:32 +04:00
|
|
|
gSems[slot].owner = team;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[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
|
|
|
|
sem_interrupt_thread(struct thread *t)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int slot;
|
|
|
|
struct thread_queue wakeup_queue;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
TRACE(("sem_interrupt_thread: called on thread %p (%d), blocked on sem 0x%x\n", t, t->id, t->sem_blocking));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-05 00:10:06 +04:00
|
|
|
if (t->state != B_THREAD_WAITING || t->sem_blocking < 0)
|
2002-07-14 14:10:22 +04:00
|
|
|
return EINVAL;
|
2002-07-21 02:52:04 +04:00
|
|
|
if ((t->sem_flags & B_CAN_INTERRUPT) == 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_SEM_NOT_INTERRUPTABLE;
|
|
|
|
|
2002-10-23 21:31:10 +04:00
|
|
|
t->next_state = B_THREAD_READY;
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
slot = t->sem_blocking % MAX_SEMS;
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
GRAB_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
if (gSems[slot].id != t->sem_blocking) {
|
2002-09-03 06:09:48 +04:00
|
|
|
panic("sem_interrupt_thread: thread 0x%lx sez it's blocking on sem 0x%lx, but that sem doesn't exist!\n", t->id, t->sem_blocking);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
wakeup_queue.head = wakeup_queue.tail = NULL;
|
2002-09-30 07:43:32 +04:00
|
|
|
if (remove_thread_from_sem(t, &gSems[slot], &wakeup_queue, EINTR) == ERR_NOT_FOUND)
|
2002-09-03 06:09:48 +04:00
|
|
|
panic("sem_interrupt_thread: thread 0x%lx not found in sem 0x%lx's wait queue\n", t->id, t->sem_blocking);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
RELEASE_SEM_LOCK(gSems[slot]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
while ((t = thread_dequeue(&wakeup_queue)) != NULL) {
|
2002-07-09 16:24:59 +04:00
|
|
|
thread_enqueue_run_q(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
/** forcibly removes a thread from a semaphores wait q. 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 sem lock held
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
remove_thread_from_sem(struct thread *t, struct sem_entry *sem, struct thread_queue *queue, int sem_errcode)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct thread *t1;
|
|
|
|
|
|
|
|
// remove the thread from the queue and place it in the supplied queue
|
|
|
|
t1 = thread_dequeue_id(&sem->q, t->id);
|
2002-09-30 07:48:59 +04:00
|
|
|
if (t != t1)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_NOT_FOUND;
|
|
|
|
sem->count += t->sem_acquire_count;
|
2002-08-05 00:10:06 +04:00
|
|
|
t->state = B_THREAD_READY;
|
2002-07-09 16:24:59 +04:00
|
|
|
t->sem_errcode = sem_errcode;
|
|
|
|
thread_enqueue(t, queue);
|
|
|
|
|
|
|
|
// now see if more threads need to be woken up
|
2002-07-21 02:52:04 +04:00
|
|
|
while (sem->count > 0 && (t1 = thread_lookat_queue(&sem->q))) {
|
2002-07-09 16:24:59 +04:00
|
|
|
int delta = min(t->sem_count, sem->count);
|
|
|
|
|
|
|
|
t->sem_count -= delta;
|
2002-09-30 07:48:59 +04:00
|
|
|
if (t->sem_count <= 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
t = thread_dequeue(&sem->q);
|
2002-08-05 00:10:06 +04:00
|
|
|
t->state = B_THREAD_READY;
|
2002-07-09 16:24:59 +04:00
|
|
|
thread_enqueue(t, queue);
|
|
|
|
}
|
|
|
|
sem->count -= delta;
|
|
|
|
}
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
for (i = 0; i < MAX_SEMS; i++) {
|
|
|
|
if (gSems[i].id != -1 && gSems[i].owner == owner) {
|
|
|
|
sem_id id = gSems[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
|
|
|
|
|
|
|
delete_sem_etc(id, 0);
|
|
|
|
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
|
|
|
|
|
|
|
sem_id
|
|
|
|
user_create_sem(int32 count, const char *uname)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-21 02:52:04 +04:00
|
|
|
if (uname != NULL) {
|
2002-07-09 16:24:59 +04:00
|
|
|
char name[SYS_MAX_OS_NAME_LEN];
|
|
|
|
int rc;
|
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if ((addr)uname >= KERNEL_BASE && (addr)uname <= KERNEL_TOP)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_VM_BAD_USER_MEMORY;
|
|
|
|
|
|
|
|
rc = user_strncpy(name, uname, SYS_MAX_OS_NAME_LEN-1);
|
2002-07-21 02:52:04 +04:00
|
|
|
if (rc < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc;
|
|
|
|
name[SYS_MAX_OS_NAME_LEN-1] = 0;
|
|
|
|
|
2002-08-03 04:41:27 +04:00
|
|
|
return create_sem_etc(count, name, team_get_current_team_id());
|
2002-07-21 02:52:04 +04:00
|
|
|
}
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
return create_sem_etc(count, NULL, team_get_current_team_id());
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
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
|
|
|
|
user_delete_sem_etc(sem_id id, status_t return_code)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return delete_sem_etc(id, return_code);
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
user_acquire_sem(sem_id id)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return user_acquire_sem_etc(id, 1, 0, 0);
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
user_acquire_sem_etc(sem_id id, int32 count, uint32 flags, bigtime_t timeout)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
flags = flags | B_CAN_INTERRUPT;
|
|
|
|
|
|
|
|
return acquire_sem_etc(id, count, flags, timeout);
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
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
|
|
|
|
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
|
|
|
|
user_get_sem_count(sem_id uid, int32* uthread_count)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 thread_count;
|
2002-09-03 06:09:48 +04:00
|
|
|
status_t rc;
|
|
|
|
int rc2;
|
2002-07-09 16:24:59 +04:00
|
|
|
rc = get_sem_count(uid, &thread_count);
|
|
|
|
rc2 = user_memcpy(uthread_count, &thread_count, sizeof(int32));
|
2002-07-21 02:52:04 +04:00
|
|
|
if (rc2 < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc2;
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
user_get_sem_info(sem_id uid, struct sem_info *uinfo, size_t sz)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct sem_info info;
|
2002-09-03 06:09:48 +04:00
|
|
|
status_t rc;
|
|
|
|
int rc2;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if ((addr)uinfo >= KERNEL_BASE && (addr)uinfo <= KERNEL_TOP)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_VM_BAD_USER_MEMORY;
|
|
|
|
|
|
|
|
rc = _get_sem_info(uid, &info, sz);
|
|
|
|
rc2 = user_memcpy(uinfo, &info, sz);
|
2002-07-21 02:52:04 +04:00
|
|
|
if (rc2 < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc2;
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
user_get_next_sem_info(team_id uteam, int32 *ucookie, struct sem_info *uinfo, size_t sz)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct sem_info info;
|
2002-09-03 06:09:48 +04:00
|
|
|
int32 cookie;
|
|
|
|
status_t rc;
|
|
|
|
int rc2;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if ((addr)uinfo >= KERNEL_BASE && (addr)uinfo <= KERNEL_TOP)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_VM_BAD_USER_MEMORY;
|
|
|
|
|
2002-09-03 06:09:48 +04:00
|
|
|
rc2 = user_memcpy(&cookie, ucookie, sizeof(int32));
|
2002-07-21 02:52:04 +04:00
|
|
|
if (rc2 < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc2;
|
2002-08-03 04:41:27 +04:00
|
|
|
rc = _get_next_sem_info(uteam, &cookie, &info, sz);
|
2002-07-09 16:24:59 +04:00
|
|
|
rc2 = user_memcpy(uinfo, &info, sz);
|
2002-07-21 02:52:04 +04:00
|
|
|
if (rc2 < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc2;
|
2002-09-03 06:09:48 +04:00
|
|
|
rc2 = user_memcpy(ucookie, &cookie, sizeof(int32));
|
2002-07-21 02:52:04 +04:00
|
|
|
if (rc2 < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc2;
|
2002-09-30 07:43:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-09-30 07:43:32 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
user_set_sem_owner(sem_id uid, team_id uteam)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-08-03 04:41:27 +04:00
|
|
|
return set_sem_owner(uid, uteam);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|