Hi there!
So I'm doing this assignment where I have to make a simple thread library. I have a double linked list of tthreads but when accessing them I get a "dereferencing pointer to incomplete type" error.
The sthread_user.c file which contains the actual definition of the thread looks like this:
Code:
/*sthread_user.c*/
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <sthread.h>
#include <sthread_user.h>
#include <sthread_ctx.h>
#include <sthread_preempt.h>
#include "dlinkedlist.h"
struct _sthread {
int tid;
int created;
int deathflag;
sthread_ctx_t *saved_ctx;
sthread_ctx_t *return_ctx;
sthread_start_func_t func;
void *args;
};
int id = 0;
list *thread_list = NULL;
/*********************************************************************/
/* Part 1: Creating and Scheduling Threads */
/*********************************************************************/
void dispatcher() {
struct _sthread *temp = thread_list->next_runnable;
sthread_ctx_t *larilas = sthread_new_blank_ctx();
(*temp->func)(temp->args);
sthread_switch(larilas, temp->return_ctx);
}
void sthread_user_init(void) {
struct _sthread *thread;
if(thread_list == NULL)
thread_list = createlst();
thread = malloc(sizeof(struct _sthread));
thread->tid = ++id;
thread->created = 0;
thread->deathflag = 0;
thread->saved_ctx = sthread_new_blank_ctx();
thread->return_ctx = sthread_new_blank_ctx();
thread->args = NULL;
insertlst(thread_list, thread);
}
sthread_t sthread_user_create(sthread_start_func_t start_routine, void *arg) {
struct _sthread *temp = thread_list->next_init;
temp->args = arg;
temp->func = start_routine;
temp->saved_ctx = sthread_new_ctx((sthread_ctx_start_func_t) dispatcher);
get_next_init(thread_list);
thread_list->next_runnable = temp;
return (sthread_t)temp;
}
void sthread_user_exit(void *ret) {
}
void sthread_user_yield(void) {
struct _sthread *temp = thread_list->next_runnable;
temp->return_ctx = sthread_new_blank_ctx();
sthread_switch(temp->return_ctx, temp->saved_ctx);
}
and the sthread_user.h like this
Code:
/*sthread_user.h*/
#ifndef STHREAD_USER_H
#define STHREAD_USER_H 1
/* Part 1: Basic Threads */
void sthread_user_init(void);
sthread_t sthread_user_create(sthread_start_func_t start_routine, void *arg);
void sthread_user_exit(void *ret);
void sthread_user_yield(void);
/* Part 2: Synchronization Primitives */
sthread_mutex_t sthread_user_mutex_init(void);
void sthread_user_mutex_free(sthread_mutex_t lock);
void sthread_user_mutex_lock(sthread_mutex_t lock);
void sthread_user_mutex_unlock(sthread_mutex_t lock);
sthread_sem_t sthread_user_sem_init(int value);
void sthread_user_sem_free(sthread_sem_t sem);
void sthread_user_sem_wait(sthread_sem_t sem);
void sthread_user_sem_post(sthread_sem_t sem);
#endif /* STHREAD_USER_H */
I know that typedef struct should be in the .h file but the assignemt clearly says we can't touch that file and the only file we can edit is the sthread_user.c.
My list code file and header files are:
Code:
/*list.h*/
#include <stdlib.h>
#include <stdio.h>
#include <sthread.h>
#include <sthread_user.h>
#include <sthread_ctx.h>
typedef struct _node{
struct _node *prev;
struct _node *next;
struct _sthread *thread;
} node;
typedef struct _list{
struct _node *first;
struct _node *last;
struct _sthread *next_init;
struct _sthread *next_runnable;
} list;
list *createlst();
void insertlst(list *list, struct _sthread *thread);
void removelst(list *list, int tid);
struct _sthread *getthread(list *list, int tid);
void get_next_init(list *list);
and the code file
Code:
/*list.c*/
#include "dlinkedlist.h"
list *createlst() {
list *new = malloc(sizeof(list));
new->first = NULL;
new->last = NULL;
new->next_init = NULL;
new->next_runnable = NULL;
return new;
}
void insertlst(list *list, struct _sthread *thread) {
node *new = malloc(sizeof(node));
new->thread = thread;
new->prev = list->last;
if(list->first == NULL) {
list->first = new;
list->next_init = new->thread;
}
else {
list->last->next = new;
}
list->last = new;
}
void get_next_init(list *list) {
node *temp;
for(temp = list->first; temp != NULL; temp = temp->next) {
if(! temp->next->thread->created ) /*the line where i get the error*/
list->next_init = temp->thread;
}
}
and i get that error in the if(! temp->next->thread->created ) line but I can't figure out why. I tried renaming and putting (()) and *'s and it just doesn't work...
Anyone?