Search code examples
key-value-storeatomicg-wan

feature request: an atomicAdd() function included in gwan.h


In the G-WAN KV options, KV_INCR_KEY will use the 1st field as the primary key.

That means there is a function which increments atomically already built in the G-WAN core to make this primary index work.

It would be good to make this function opened to be used by servlets, i.e. included in gwan.h.

By doing so, ANSI C newbies like me could benefit from it.


Solution

  • Thanks for Gil's helpful guidance.
    Now, I can do it by myself.
    I change the code in persistence.c, as below:
    firstly, i changed the definition of val in data to volatile.

    //data[0]->val++;  
    //xbuf_xcat(reply, "Value: %d", data[0]->val);  
    int new_count, loops=50000000, time1, time2, time;  
    
    time1=getus();
    for(int i; i<loops; i++){
        new_count = __sync_add_and_fetch(&data[0]->val, 1);
    }
    time2=getus();
    
    time=loops/(time2-time1);
    time=time*1000;
    
    xbuf_xcat(reply, "Value: %d, time: %d incr_ops/msec", new_count, time);
    

    I got 52,000 incr_operations/msec with my old E2180 CPU.
    So, with GCC compiler I can do it by myself.
    thanks again.