Search code examples
cpthreadsposix

error: expected expression before ‘{’ token in pthread rw_lock


So i have this code of a node in an kinda of a data structure and the node has a read write lock to initialize the lock i used pthread_rwlock_init before using pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

the thing is for some reason it gives an error saying that it was expected an expression before the PTHREAD_RWLOCK_INITIALIZER and i dont understand what it is because i know im not missing any ;

Code:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "state.h"
#include <pthread.h>

typedef struct inode_t {    
    type nodeType;
    union Data data;
    // ******************NEW ATTRIBUTE THAT INLCUDES THE LOCK *******************************
    pthread_rwlock_t lock;
} inode_t;

void inode_table_init() {
    for (int i = 0; i < INODE_TABLE_SIZE; i++) {
        inode_table[i].nodeType = T_NONE;
        inode_table[i].data.dirEntries = NULL;
        inode_table[i].data.fileContents = NULL;
        // ****************** NEW *******************************
        // Initializes the lock
        pthread_rwlock_init(&inode_table[i].lock ,NULL);
        inode_table[i].lock = PTHREAD_RWLOCK_INITIALIZER;
    }
}

int inode_create(type nType) {
    /* Used for testing synchronization speedup */
    insert_delay(DELAY);

    for (int inumber = 0; inumber < INODE_TABLE_SIZE; inumber++) {
        if (inode_table[inumber].nodeType == T_NONE) {
            inode_table[inumber].nodeType = nType;
            
            pthread_rwlock_init(&inode_table[inumber].lock ,NULL);
            inode_table[inumber].lock = PTHREAD_RWLOCK_INITIALIZER;

            if (nType == T_DIRECTORY) {
                /* Initializes entry table */
                inode_table[inumber].data.dirEntries = malloc(sizeof(DirEntry) * MAX_DIR_ENTRIES);
                
                for (int i = 0; i < MAX_DIR_ENTRIES; i++) {
                    inode_table[inumber].data.dirEntries[i].inumber = FREE_INODE;
                }
            }
            else {
                inode_table[inumber].data.fileContents = NULL;
            }
            return inumber;
        }
    }
    return FAIL;
}

Well it doesnt matter what the function does what it matters is that im having a strange error in the lines that im initializing the locks

Error:

fs/state.c: In function ‘inode_table_init’:
fs/state.c:34:31: error: expected expression before ‘{’ token
         inode_table[i].lock = PTHREAD_RWLOCK_INITIALIZER;
                               ^
fs/state.c: In function ‘inode_create’:
fs/state.c:81:41: error: expected expression before ‘{’ token
             inode_table[inumber].lock = PTHREAD_RWLOCK_INITIALIZER;

Well any help would be appreciated.


Solution

  • You don't need those assignments. From the POSIX spec:

    In cases where default read-write lock attributes are appropriate, the macro PTHREAD_RWLOCK_INITIALIZER can be used to initialise read-write locks that are statically allocated. The effect is equivalent to dynamic initialisation by a call to pthread_rwlock_init() with the parameter attr specified as NULL, except that no error checks are performed.

    You can only use it in a variable initialization, not an assignment. And since it's equivalent to the calls on the lines before, it's redundant even it worked.