1

I am trying to initialize a SIMPLEQ_HEAD in C and I and getting the following error:

# gcc -o semaphore semaphore.c 
semaphore.c: In function `createSemaphore':
semaphore.c:10: syntax error before `{'

My semaphore.c class looks like:

#include <stdio.h>
#include "semaphore.h"

semaphore_t* createSemaphore( int initialCount )
{
    semaphore_t* sem;
    sem = (semaphore_t *) malloc( sizeof( semaphore_t ) );
    sem->head = SIMPLEQ_HEAD_INITIALIZER( sem->head );
    sem->count = initialCount;      // set the semaphores initial count
    return sem;
}
...
int main()
{

    semaphore_t* my_semaphore = createSemaphore( 5 );    
    return 0;
}

And my semaphore.h looks like:

#ifndef SEMAPHORE_H
#define SEMAPHORE_H

#include <sys/queue.h>
#include <pthread.h>
#include <stdlib.h>

struct entry_thread {
    int threadid;
    SIMPLEQ_ENTRY(entry_thread) next;
} *np;

struct semaphore {
    int count;
    pthread_mutex_t mutex;
    pthread_cond_t flag;
    SIMPLEQ_HEAD(queuehead, entry_thread) head;
};

typedef struct semaphore semaphore_t;

semaphore_t* createSemaphore( int initialCount );
void destroySemaphore( semaphore_t* sem );
void down( semaphore_t* sem );
void up( semaphore_t* sem );

#endif

I'm new to C and using SIMPLEQ so if anybody is experienced in this I would greatly appreciate some help.

For your reference, SIMPLEQ_HEAD's man page is: http://www.manualpages.de/OpenBSD/OpenBSD-5.0/man3/SIMPLEQ_HEAD.3.html

Zach
  • 4,555
  • 9
  • 31
  • 52
  • I didn't read it all, but what jumps me right into the face: why the heck implement a *semaphore* on top of *mutexes* and *condition variables*? A *semaphore* is so much simpler than a *mutex* and is already available in POSIX. (`sem_t`, see [`sem_init(3)`](http://linux.die.net/man/3/sem_init)) –  Oct 31 '15 at 20:01
  • Unrelated, but: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – user4520 Oct 31 '15 at 20:01
  • @FelixPalmen it's for my operating systems course, so we are learning to implement it on our own. – Zach Oct 31 '15 at 20:12
  • Also, there's not really "classes" in C. Your `semaphore_t` could be considered conceptually a "class" because you have a *create* "method" and a bunch of methods accepting a *this pointer* as the first argument, but in the context of C, it still isn't a class, because C doesn't know such a thing. –  Oct 31 '15 at 20:12
  • @szczurcio thanks :) I'll look into that – Zach Oct 31 '15 at 20:12
  • @FelixPalmen thanks for clearing that up as well. I know on its own C isn't OOP, but conceptually calling it a class made sense to me – Zach Oct 31 '15 at 20:13
  • @Zach well, to get closer to your problem ... WTF is `SIMPLEQ_HEAD`? This is **not** standard C. You should show the definition if you want help ... –  Oct 31 '15 at 20:13
  • @FelixPalmen I literally just learned about it today for the assignment! I posted a link to the man page for it in my question. It's pretty much a set of macros for a queue implementation – Zach Oct 31 '15 at 20:15
  • @Zach I see -- that's some "great" documentation there. My linux box doesn't have this. Are you required to use it? Implementing a queue without predefined macros isn't rocket science after all.... –  Oct 31 '15 at 20:21
  • Haha yeah. This class really stresses the importance of learning from man pages. SIMPLEQ is a requirement for the assignment, and it's from UNIX so I'm guessing if you wanted it on your Linux box you'd need to add some stuff – Zach Oct 31 '15 at 20:23
  • @Zach not even that, the manpage says it's from 4.4 BSD, which is already a derivate of Unix -- and it isn't standardized anywhere (no "conforming to" section in the manpage). Therefore I'd never use it in production code meant to be portable. But that being said, if it *is* your requirement to use it, maybe somebody who used it before could help. –  Oct 31 '15 at 20:31
  • Ok. Hopefully someone comes around with experience in SIMPLEQ – Zach Oct 31 '15 at 20:37

1 Answers1

0

After some testing, I realized

sem->head = SIMPLEQ_HEAD_INITIALIZER( sem->head );

is a static operation, so it wouldn't work as I'm dynamically creating semaphores/SIMPLEQs.

What I changed that line to instead is

SIMPLEQ_INIT( &(sem->head) ); // note that you don't set anything equal to the result

which dynamically takes care of the initialization, and solved my problem.

Zach
  • 4,555
  • 9
  • 31
  • 52