Skip to content
Open

1 #3

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions lwt/lwt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* lwt.c
* Pradeep, Yang
*/

#ifndef __LWT_H__
#define __LWT_H__
#include <pthread.h>
#define MAX_THD 64
#define LWT_NULL NULL

lwt_t lwt_pool[MAX_THD];// a thread pool for reuse, or a table to get tcb pointer by its id
int Queue[MAX_THD];// a queue to store living thread
int queue_length=0;

lwt_t current_thd;//global pointer to current executing thread

int runnable_num=1;
int blocked_num=0;
int zombies_num=0;//global variable for lwt_info


lwt_t lwt_create(lwt_fn_t fn, void *data){
// set stack, add id to queue and update length
}


void* lwt_join(lwt_t thd_handle){
// wait for a thread
}

void* lwt_die(void *ret){
// kill current thread
}

void lwt_yield(lwt_t thd_handle){
// yield current thread and call schedule funtion
}

lwt_t lwt_current(){
// return a pointer to current thread
return current_thd;
}//done

int lwt_id(lwt_t thd){
// return the unique id for the thread
return thd->id;
}//done

int lwt_info(lwt_info_t t){
// debugging helper
switch(t){
case LWT_INFO_NTHD_RUNNABLE:
return runnable_num;
case LWT_INFO_NTHD_BLOCKED:
return blocked_num;
case LWT_INFO_NTHD_ZOMBIES:
return zombies_num;
case default:
return 0;
}
}//done

/*
* Internal functions.
*/
void __ lwt_schedule(void){
// scheduling
}

void __lwt_dispatch(lwt_t next, lwt_t current){
// context switch from current to next
}

void __lwt_trampoline(){
// ?
}

void *__lwt_stack_get(void){
// allocate a new stack for a new lwt
}

void *__lwt_stack_return(void *stk){
// recover memory for a lwt's stack for reuse
}

46 changes: 42 additions & 4 deletions lwt/lwt.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
/* lwt.h
* Pradeep, Yang
*/

#ifndef __LWT_H__
#define __LWT_H__

#define LWT_NULL NULL

typedef void* (*lwt_fn_t) (void *);

typedef enum{
RUN=1,
WAIT,
READY,
COMPLETE
}lwt_status_t;

/* data structures */
struct lwt_tcb{ //thread control block;
void* ip;
void* sp;
void* bp;
void* return_value;
lwt_fn_t fn;
void* data;
int id;//we may keep a lwt_t array or pool to store all lwt with id as its index, and a current queue to store ids for all currently living lwt
lwt_status_t tcb_status;
int queue_index;
};

typedef struct lwt_tcb* lwt_t; //a pointer to tcb, use it as pthread_t
typedef struct lwt_tcb tcb;

typedef enum{
LWT_INFO_NTHD_RUNNABLE=0,
LWT_INFO_NTHD_BLOCKED,
LWT_INFO_NTHD_ZOMBIES
}lwt_info_t;

/*
* lightweight thread APIs.
*/

void lwt_init(unsigned int thread_pool_size);

lwt_t lwt_create(lwt_fn_t fn, void* data);

void* lwt_join(lwt_t thd_handle);

void* lwt_die(void *ret);
void lwt_die(void *ret);

void lwt_yield(lwt_t thd_handle);

Expand All @@ -24,14 +61,15 @@ int lwt_info(lwt_info_t t);
/*
* Internal functions.
*/
void __ lwt_schedule(void);
void __lwt_schedule(void);

void __lwt_dispatch(lwt_t next, lwt_t current);
void __lwt_dispatch(lwt_t current, lwt_t next);

void __lwt_trampoline();

void *__lwt_stack_get(void);

void *__lwt_stack_return(void *stk);
void __lwt_stack_return(void *stk);

void __lwt_dequeue(lwt_t thd);
#endif
Loading