J
JS
I use winXP and have installed Cygwin. I use Dev-C++ and the Cygwin
compiler, but for some reason I can't compile this code:
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
typedef int othread_t;
typedef struct _othread_attr_t {
} othread_attr_t;
#define STACK_SIZE 1024
struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[STACK_SIZE];
};
static struct pcb first_thread;
struct pcb *current = &first_thread;
struct pcb *ready = NULL;
int othread_create (othread_t *threadp,
const othread_attr_t *attr,
void *(*start_routine) (void *),
void *arg)
{
int *state_pointer;
int lokalt_reserveret;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));
if(pcb_pointer == NULL) {
exit(-1);
}
pcb_pointer->start_routine = start_routine;
pcb_pointer->arg = arg;
if(setjmp(pcb_pointer->state)) {
current->start_routine(current->arg);
printf("En tråd har returneret - alt stoppes\n");
exit(0);
}
state_pointer = (int *) &pcb_pointer->state;
lokalt_reserveret = state_pointer[JB_BP] - state_pointer[JB_SP];
state_pointer[JB_BP] = (int) (pcb_pointer->stak + STACK_SIZE);
state_pointer[JB_SP] = (int) state_pointer[JB_BP] - lokalt_reserveret;
ready = pcb_pointer;
*threadp = (int) pcb_pointer;
return 0;
}
int othread_yield (void)
{
if(ready != NULL) {
struct pcb *old = current;
current = ready;
ready = old;
if(setjmp(old->state) == 0) {
longjmp(current->state, 1);
}
}
return 0;
}
void *other(void *arg)
{
printf("Hello world from other\n");
othread_yield();
printf("other still going strong\n");
othread_yield();
printf("Godbye world from other\n");
return NULL;
}
int main(void)
{
othread_t t1;
othread_create(&t1, NULL, other, NULL);
printf("Hello world from main\n");
othread_yield();
printf("main still going strong\n");
othread_yield();
printf("Godbye world from main\n");
othread_yield();
printf("Is never reached\n");
return 0;
}
The error I get is:
56 H:\thread\thread.c `JB_BP' undeclared (first use in this function)
(Each undeclared identifier is reported only once
for each function it appears in.)
56 H:\thread\thread.c `JB_SP' undeclared (first use in this function)
make H:\thread\make *** [thread.o] Error 1
when I compile the same code on Linux it works fine! But I am sure that I
use Cygwin so that should be as good as using Linux.
Hope someone can help
compiler, but for some reason I can't compile this code:
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
typedef int othread_t;
typedef struct _othread_attr_t {
} othread_attr_t;
#define STACK_SIZE 1024
struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[STACK_SIZE];
};
static struct pcb first_thread;
struct pcb *current = &first_thread;
struct pcb *ready = NULL;
int othread_create (othread_t *threadp,
const othread_attr_t *attr,
void *(*start_routine) (void *),
void *arg)
{
int *state_pointer;
int lokalt_reserveret;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));
if(pcb_pointer == NULL) {
exit(-1);
}
pcb_pointer->start_routine = start_routine;
pcb_pointer->arg = arg;
if(setjmp(pcb_pointer->state)) {
current->start_routine(current->arg);
printf("En tråd har returneret - alt stoppes\n");
exit(0);
}
state_pointer = (int *) &pcb_pointer->state;
lokalt_reserveret = state_pointer[JB_BP] - state_pointer[JB_SP];
state_pointer[JB_BP] = (int) (pcb_pointer->stak + STACK_SIZE);
state_pointer[JB_SP] = (int) state_pointer[JB_BP] - lokalt_reserveret;
ready = pcb_pointer;
*threadp = (int) pcb_pointer;
return 0;
}
int othread_yield (void)
{
if(ready != NULL) {
struct pcb *old = current;
current = ready;
ready = old;
if(setjmp(old->state) == 0) {
longjmp(current->state, 1);
}
}
return 0;
}
void *other(void *arg)
{
printf("Hello world from other\n");
othread_yield();
printf("other still going strong\n");
othread_yield();
printf("Godbye world from other\n");
return NULL;
}
int main(void)
{
othread_t t1;
othread_create(&t1, NULL, other, NULL);
printf("Hello world from main\n");
othread_yield();
printf("main still going strong\n");
othread_yield();
printf("Godbye world from main\n");
othread_yield();
printf("Is never reached\n");
return 0;
}
The error I get is:
56 H:\thread\thread.c `JB_BP' undeclared (first use in this function)
(Each undeclared identifier is reported only once
for each function it appears in.)
56 H:\thread\thread.c `JB_SP' undeclared (first use in this function)
make H:\thread\make *** [thread.o] Error 1
when I compile the same code on Linux it works fine! But I am sure that I
use Cygwin so that should be as good as using Linux.
Hope someone can help