c-semantics icon indicating copy to clipboard operation
c-semantics copied to clipboard

when there is gcc's atomic definitions, it can't work

Open Lycbel opened this issue 6 years ago • 1 comments

when compile sophia https://github.com/pmwkaa/sophia commit 669d57ba91bcb3593d8528704b2d790a97ffadd6 kcc can't use the gcc defined functions like __sync_lock_test_and_set for atomic functionality maybe RV-match should think about it this is a minimized input for it

wget https://github.com/Lycbel/cs510Files/blob/master/report7/sophia/sophia.zip?raw=true -O sophia.zip
unzip sophia.zp
cd sophia
bash run.sh

Lycbel avatar Dec 13 '18 02:12 Lycbel

this is another project having problem because of aotmic disque commit 0192ba7e1cda157024229962b7bee1c6e86d771b the code main.c is the minimied input for it, it will based on the flag to determine the function definition it shows if there is flag __ATOMIC_RELAXED the pre-processed code will be (gcc case)

do { size_t _n = (size+(sizeof(size_t))); if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); if (zmalloc_thread_safe) { __atomic_add_fetch(&used_memory,(size+(sizeof(size_t))),0); } else { used_memory += _n; } } while(0);

if not it will be(kcc case)

do { size_t _n = (size+(sizeof(size_t))); if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); if (zmalloc_thread_safe) { do { pthread_mutex_lock(& &used_memory_mutex); used_memory += (size+(sizeof(size_t))); pthread_mutex_unlock(& &used_memory_mutex); } while(0); } else { used_memory += _n; } } while(0);

then for the kcc version it will give the error (if we use gcc to run this version, it will also have same error)

lock1.c:9:24: error: lvalue required as unary ‘&’ operand
     pthread_mutex_lock(&mutex); \

to reproduce the problem when compiling disque

bash ./precheck.sh
echo "kcc main.c -pthread -std=c99"
kcc main.c -pthread -std=c99
echo "--------------------------------------------"
echo "gcc main.c -pthread -std=c99"
gcc main.c -pthread -std=c99

all these flags are missing(including the one we need in disque)

__ATOMIC_RELAXED
__ATOMIC_CONSUME
__ATOMIC_ACQUIRE
__ATOMIC_RELEASE
__ATOMIC_ACQ_REL
__ATOMIC_SEQ_CST

main.c

#include <stdlib.h>
#include <pthread.h>

#if defined(__ATOMIC_RELAXED)
//Implementation using __atomic macros.
#define atomicIncr(var,count,mutex) __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED)
#elif defined(HAVE_ATOMIC)
//Implementation using __sync macros.

#define atomicIncr(var,count,mutex) __sync_add_and_fetch(&var,(count))

#else
//Implementation using pthread mutex.

#define atomicIncr(var,count,mutex) do { \
    pthread_mutex_lock(&mutex); \
    var += (count); \
    pthread_mutex_unlock(&mutex); \
} while(0)

#endif

#define update_zmalloc_stat_alloc(__n) do { \
    size_t _n = (size+(sizeof(size_t))); \
    if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
    if (zmalloc_thread_safe) { \
        atomicIncr(used_memory,__n,&used_memory_mutex); \
    } else { \
        used_memory += _n; \
    } \
} while(0)

static int zmalloc_thread_safe = 0;
static size_t used_memory = 0;
pthread_mutex_t used_memory_mutex;


void *zmalloc(size_t size) {
    void *ptr = malloc(size+1);
    *((size_t*)ptr) = size;
    update_zmalloc_stat_alloc(size+1);
    return (char*)ptr+1;
}

int main(){
return 0;
}

Lycbel avatar Dec 13 '18 07:12 Lycbel