Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 1609 → Rev 1610

//uspace/trunk/libc/include/psthread.h
40,10 → 40,13
(c)->tls = (sysarg_t) (ptls);
#endif /* context_set */
 
#define PSTHREAD_SERIALIZED 1
 
typedef enum {
PS_TO_MANAGER,
PS_FROM_MANAGER,
PS_PREEMPT
PS_PREEMPT,
PS_FROM_DEAD
} pschange_type;
 
typedef sysarg_t pstid_t;
75,6 → 78,8
void psthread_add_manager(pstid_t psthrid);
void psthread_remove_manager(void);
pstid_t psthread_get_id(void);
void psthread_inc_sercount(void);
void psthread_dec_sercount(void);
 
static inline int psthread_schedule_next() {
return psthread_schedule_next_adv(PS_PREEMPT);
//uspace/trunk/libc/include/async.h
37,7 → 37,11
typedef ipc_callid_t aid_t;
typedef void (*async_client_conn_t)(ipc_callid_t callid, ipc_call_t *call);
 
int async_manager(void);
static inline void async_manager(void)
{
psthread_schedule_next_adv(PS_TO_MANAGER);
}
 
ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs);
static inline ipc_callid_t async_get_call(ipc_call_t *data)
{
57,7 → 61,7
*
* @return Return code of message
*/
static inline ipcarg_t sync_send_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t *r1, ipcarg_t *r2)
static inline ipcarg_t async_req_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t *r1, ipcarg_t *r2)
{
ipc_call_t result;
ipcarg_t rc;
70,9 → 74,9
*r2 = IPC_GET_ARG2(result);
return rc;
}
#define sync_send(phoneid, method, arg1, r1) sync_send_2(phoneid, method, arg1, 0, r1, 0)
#define async_req(phoneid, method, arg1, r1) async_req_2(phoneid, method, arg1, 0, r1, 0)
 
static inline ipcarg_t sync_send_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
static inline ipcarg_t async_req_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, ipcarg_t arg3, ipcarg_t *r1,
ipcarg_t *r2, ipcarg_t *r3)
{
102,6 → 106,20
int _async_init(void);
 
 
/* Primitve functions for IPC communication */
void async_msg_3(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
ipcarg_t arg3);
void async_msg_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2);
#define async_msg(ph,m,a1) async_msg_2(ph,m,a1,0)
 
static inline void async_serialize_start(void)
{
psthread_inc_sercount();
}
static inline void async_serialize_end(void)
{
psthread_dec_sercount();
}
 
extern atomic_t async_futex;
 
#endif
//uspace/trunk/libc/include/ipc/ipc.h
81,16 → 81,4
extern int ipc_unregister_irq(int irq);
extern int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1);
 
 
/* Primitve functions for IPC communication */
void send_call_3(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
ipcarg_t arg3);
void send_call_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2);
#define send_call(ph,m,a1) send_call_2(ph,m,a1,0)
/* These functions never preempt */
void nsend_call_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, ipcarg_t arg3);
void nsend_call_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2);
#define nsend_call(ph,m,a1) nsend_call_2(ph,m,a1,0)
 
#endif
//uspace/trunk/libc/generic/psthread.c
42,6 → 42,7
#endif
 
static LIST_INITIALIZE(ready_list);
static LIST_INITIALIZE(serialized_list);
static LIST_INITIALIZE(manager_list);
 
static void psthread_exit(void) __attribute__ ((noinline));
48,6 → 49,12
static void psthread_main(void);
 
static atomic_t psthread_futex = FUTEX_INITIALIZER;
/** Count of real threads that are in async_serialized mode */
static int serialized_threads; /* Protected by async_futex */
/** Thread-local count of serialization. If >0, we must not preempt */
static __thread serialization_count;
/** Counter of threads residing in async_manager */
static int threads_in_manager;
 
/** Setup PSthread information into TCB structure */
psthread_data_t * psthread_setup()
77,35 → 84,12
free(pt);
}
 
/** Function to preempt to other pseudo thread without adding
* currently running pseudo thread to ready_list.
*/
void psthread_exit(void)
{
psthread_data_t *pt;
 
futex_down(&psthread_futex);
 
if (!list_empty(&ready_list))
pt = list_get_instance(ready_list.next, psthread_data_t, link);
else if (!list_empty(&manager_list))
pt = list_get_instance(manager_list.next, psthread_data_t, link);
else {
printf("Cannot find suitable psthread to run.\n");
_exit(0);
}
list_remove(&pt->link);
futex_up(&psthread_futex);
 
context_restore(&pt->ctx);
/* Never reached */
}
 
/** Function that is called on entry to new uspace thread */
void psthread_main(void)
{
psthread_data_t *pt = __tcb_get()->pst_data;
 
serialization_count = 0; // TODO: WHY HERE?
pt->retval = pt->func(pt->arg);
 
pt->finished = 1;
112,7 → 96,7
if (pt->waiter)
list_append(&pt->waiter->link, &ready_list);
 
psthread_exit();
psthread_schedule_next_adv(PS_FROM_DEAD);
}
 
/** Schedule next userspace pseudo thread.
127,7 → 111,7
*/
int psthread_schedule_next_adv(pschange_type ctype)
{
psthread_data_t *pt;
psthread_data_t *srcpt, *dstpt;
int retval = 0;
futex_down(&psthread_futex);
135,33 → 119,61
if (ctype == PS_PREEMPT && list_empty(&ready_list))
goto ret_0;
 
if (ctype == PS_FROM_MANAGER && list_empty(&ready_list)) {
goto ret_0;
if (ctype == PS_FROM_MANAGER) {
if (list_empty(&ready_list) && list_empty(&serialized_list))
goto ret_0;
/* Do not preempt if there is not sufficient count of thread managers */
if (list_empty(&serialized_list) && threads_in_manager <= serialized_threads) {
goto ret_0;
}
}
/* If we are going to manager and none exists, create it */
while (ctype == PS_TO_MANAGER && list_empty(&manager_list)) {
futex_up(&psthread_futex);
async_create_manager();
futex_down(&psthread_futex);
if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
while (list_empty(&manager_list)) {
futex_up(&psthread_futex);
async_create_manager();
futex_down(&psthread_futex);
}
}
if (ctype != PS_FROM_DEAD) {
/* Save current state */
srcpt = __tcb_get()->pst_data;
if (!context_save(&srcpt->ctx)) {
if (serialization_count)
srcpt->flags &= ~PSTHREAD_SERIALIZED;
return 1; // futex_up already done here
}
 
pt = __tcb_get()->pst_data;
if (!context_save(&pt->ctx))
return 1; // futex_up already done here
/* Save myself to correct run list */
if (ctype == PS_PREEMPT)
list_append(&srcpt->link, &ready_list);
else if (ctype == PS_FROM_MANAGER) {
list_append(&srcpt->link, &manager_list);
threads_in_manager--;
} /* If ctype == PS_TO_MANAGER, don't save ourselves to any list, we should
* already be somewhere, or we will be lost */
}
 
if (ctype == PS_PREEMPT)
list_append(&pt->link, &ready_list);
else if (ctype == PS_FROM_MANAGER)
list_append(&pt->link, &manager_list);
if (ctype == PS_TO_MANAGER)
pt = list_get_instance(manager_list.next,psthread_data_t, link);
else
pt = list_get_instance(ready_list.next, psthread_data_t, link);
list_remove(&pt->link);
/* Choose new thread to run */
if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
dstpt = list_get_instance(manager_list.next,psthread_data_t, link);
if (serialization_count && ctype == PS_TO_MANAGER) {
serialized_threads++;
srcpt->flags |= PSTHREAD_SERIALIZED;
}
threads_in_manager++;
} else {
if (!list_empty(&serialized_list)) {
dstpt = list_get_instance(serialized_list.next, psthread_data_t, link);
serialized_threads--;
} else
dstpt = list_get_instance(ready_list.next, psthread_data_t, link);
}
list_remove(&dstpt->link);
 
futex_up(&psthread_futex);
context_restore(&pt->ctx);
context_restore(&dstpt->ctx);
 
ret_0:
futex_up(&psthread_futex);
182,13 → 194,10
/* Handle psthrid = Kernel address -> it is wait for call */
pt = (psthread_data_t *) psthrid;
 
if (!pt->finished) {
mypt = __tcb_get()->pst_data;
if (context_save(&((psthread_data_t *) mypt)->ctx)) {
pt->waiter = (psthread_data_t *) mypt;
psthread_exit();
}
}
/* TODO */
printf("join unsupported\n");
_exit(1);
 
retval = pt->retval;
 
free(pt->stack);
223,6 → 232,7
pt->func = func;
pt->finished = 0;
pt->waiter = NULL;
pt->flags = 0;
 
context_save(&pt->ctx);
context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(),
238,7 → 248,10
 
pt = (psthread_data_t *) psthrid;
futex_down(&psthread_futex);
list_append(&pt->link, &ready_list);
if ((pt->flags & PSTHREAD_SERIALIZED))
list_append(&pt->link, &serialized_list);
else
list_append(&pt->link, &ready_list);
futex_up(&psthread_futex);
}
 
271,3 → 284,21
{
return (pstid_t)__tcb_get()->pst_data;
}
 
/** Disable preemption
*
* If the thread wants to send several message in row and does not want
* to be preempted, it should start async_serialize_start() in the beginning
* of communication and async_serialize_end() in the end. If it is a
* true multithreaded application, it should protect the communication channel
* by a futex as well. Interrupt messages will can still be preempted.
*/
void psthread_inc_sercount(void)
{
serialization_count++;
}
 
void psthread_dec_sercount(void)
{
serialization_count--;
}
//uspace/trunk/libc/generic/ipc.c
437,26 → 437,3
return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1);
}
 
/* Primitive functions for simple communication */
void send_call_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, ipcarg_t arg3)
{
ipc_call_async_3(phoneid, method, arg1, arg2, arg3, NULL, NULL, 1);
}
 
void send_call_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2)
{
ipc_call_async_2(phoneid, method, arg1, arg2, NULL, NULL, 1);
}
 
void nsend_call_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, ipcarg_t arg3)
{
ipc_call_async_3(phoneid, method, arg1, arg2, arg3, NULL, NULL, 0);
}
 
void nsend_call_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2)
{
ipc_call_async_2(phoneid, method, arg1, arg2, NULL, NULL, 0);
}
 
//uspace/trunk/libc/generic/async.c
134,8 → 134,11
void (*cthread)(ipc_callid_t,ipc_call_t *);
} connection_t;
 
 
/** Identifier of incoming connection handled by current thread */
__thread connection_t *PS_connection;
/** If true, it is forbidden to use async_req functions and
* all preemption is disabled */
__thread int in_interrupt_handler;
 
static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
225,6 → 228,7
awaiter_t *cur;
 
wd->timedout = 0;
wd->inlist = 1;
 
tmp = timeout_list.next;
while (tmp != &timeout_list) {
294,9 → 298,6
*/
conn = PS_connection;
 
if (usecs < 0) /* TODO: let it get through the ipc_call once */
return 0;
 
futex_down(&async_futex);
 
if (usecs) {
307,10 → 308,9
}
/* If nothing in queue, wait until something appears */
while (list_empty(&conn->msg_queue)) {
if (usecs) {
conn->wdata.inlist = 1;
if (usecs)
insert_timeout(&conn->wdata);
}
 
conn->wdata.active = 0;
psthread_schedule_next_adv(PS_TO_MANAGER);
/* Futex is up after getting back from async_manager
362,7 → 362,6
/* Setup thread local connection pointer */
PS_connection = (connection_t *)arg;
PS_connection->cthread(PS_connection->callid, &PS_connection->call);
 
/* Remove myself from connection hash table */
futex_down(&async_futex);
key = PS_connection->in_phone_hash;
435,7 → 434,9
/* Unrouted call - do some default behaviour */
switch (IPC_GET_METHOD(*call)) {
case IPC_M_INTERRUPT:
in_interrupt_handler = 1;
(*interrupt_received)(callid,call);
in_interrupt_handler = 0;
return;
case IPC_M_CONNECT_ME_TO:
/* Open new connection with thread etc. */
485,7 → 486,7
}
 
/** Endless loop dispatching incoming calls and answers */
int async_manager(void)
static int async_manager_worker(void)
{
ipc_call_t call;
ipc_callid_t callid;
521,8 → 522,9
continue;
}
 
if (callid & IPC_CALLID_ANSWERED)
if (callid & IPC_CALLID_ANSWERED) {
continue;
}
 
handle_call(callid, &call);
}
537,9 → 539,10
*/
static int async_manager_thread(void *arg)
{
in_interrupt_handler = 0; // TODO: Handle TLS better
futex_up(&async_futex); /* async_futex is always locked when entering
* manager */
async_manager();
async_manager_worker();
}
 
/** Add one manager to manager list */
607,6 → 610,11
{
amsg_t *msg;
 
if (in_interrupt_handler) {
printf("Cannot send asynchronou request in interrupt handler.\n");
_exit(1);
}
 
msg = malloc(sizeof(*msg));
msg->done = 0;
msg->dataptr = dataptr;
628,6 → 636,11
{
amsg_t *msg;
 
if (in_interrupt_handler) {
printf("Cannot send asynchronou request in interrupt handler.\n");
_exit(1);
}
 
msg = malloc(sizeof(*msg));
msg->done = 0;
msg->dataptr = dataptr;
698,8 → 711,6
 
msg->wdata.ptid = psthread_get_id();
msg->wdata.active = 0;
msg->wdata.inlist = 1;
 
insert_timeout(&msg->wdata);
 
/* Leave locked async_futex when entering this function */
725,12 → 736,16
{
amsg_t *msg;
if (in_interrupt_handler) {
printf("Cannot call async_usleep in interrupt handler.\n");
_exit(1);
}
 
msg = malloc(sizeof(*msg));
if (!msg)
return;
 
msg->wdata.ptid = psthread_get_id();
msg->wdata.inlist = 1;
msg->wdata.active = 0;
 
gettimeofday(&msg->wdata.expires, NULL);
756,3 → 771,15
{
interrupt_received = conn;
}
 
/* Primitive functions for simple communication */
void async_msg_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
ipcarg_t arg2, ipcarg_t arg3)
{
ipc_call_async_3(phoneid, method, arg1, arg2, arg3, NULL, NULL, !in_interrupt_handler);
}
 
void async_msg_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2)
{
ipc_call_async_2(phoneid, method, arg1, arg2, NULL, NULL, !in_interrupt_handler);
}
//uspace/trunk/libc/generic/io/stream.c
64,7 → 64,7
size_t i = 0;
 
while (i < count) {
if (sync_send_2(streams[0].phone, CONSOLE_GETCHAR, 0, 0, &r0, &r1) < 0) {
if (async_req_2(streams[0].phone, CONSOLE_GETCHAR, 0, 0, &r0, &r1) < 0) {
return -1;
}
((char *)buf)[i++] = r0;
78,7 → 78,7
ipcarg_t r0,r1;
 
for (i = 0; i < count; i++)
send_call(streams[1].phone, CONSOLE_PUTCHAR, ((const char *)buf)[i]);
async_msg(streams[1].phone, CONSOLE_PUTCHAR, ((const char *)buf)[i]);
return count;
}
130,7 → 130,7
fd_t open(const char *fname, int flags)
{
int c = 0;
 
while (((streams[c].w) || (streams[c].r)) && (c < FDS))
c++;
if (c == FDS)
159,6 → 159,8
 
ssize_t write(int fd, const void *buf, size_t count)
{
// __SYSCALL3(SYS_IO, 1, (sysarg_t)buf, (sysarg_t) count);
// return count;
if (fd < FDS)
return streams[fd].w(streams[fd].param, buf, count);