+bool getreadlock(void * ptr) {
+#ifdef RAW
+
+#elif defined THREADSIMULATE
+ int numofcore = pthread_getspecific(key);
+
+ int rc = pthread_rwlock_tryrdlock(&rwlock_tbl);
+ printf("[getreadlock, %d] getting the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ if(EBUSY == rc) {
+ return false;
+ }
+ if(!RuntimeHashcontainskey(locktbl, (int)ptr)) {
+ // no locks for this object
+ // first time to operate on this shared object
+ // create a lock for it
+ rc = pthread_rwlock_unlock(&rwlock_tbl);
+ printf("[getreadlock, %d] release the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ pthread_rwlock_t* rwlock = (pthread_rwlock_t *)RUNMALLOC(sizeof(pthread_rwlock_t));
+ memcpy(rwlock, &rwlock_init, sizeof(pthread_rwlock_t));
+ rc = pthread_rwlock_init(rwlock, NULL);
+ printf("[getreadlock, %d] initialize the rwlock for object %d: %d error: \n", numofcore, (int)ptr, rc, strerror(rc));
+ rc = pthread_rwlock_trywrlock(&rwlock_tbl);
+ printf("[getreadlock, %d] getting the write lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ if(EBUSY == rc) {
+ return false;
+ } else {
+ RuntimeHashadd(locktbl, (int)ptr, (int)rwlock);
+ rc = pthread_rwlock_unlock(&rwlock_tbl);
+ printf("[getreadlock, %d] release the write lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ }
+ //rc = pthread_rwlock_rdlock(&rwlock);
+ rc = pthread_rwlock_tryrdlock(rwlock);
+ printf("[getreadlock, %d] getting read lock for object %d: %d error: \n", numofcore, (int)ptr, rc, strerror(rc));
+ if(EBUSY == rc) {
+ return false;
+ } else {
+ return true;
+ }
+ } else {
+ pthread_rwlock_t* rwlock_obj = NULL;
+ RuntimeHashget(locktbl, (int)ptr, (int*)&rwlock_obj);
+ rc = pthread_rwlock_unlock(&rwlock_tbl);
+ printf("[getreadlock, %d] release the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ //int rc_obj = pthread_rwlock_rdlock(&rwlock_obj);
+ int rc_obj = pthread_rwlock_tryrdlock(rwlock_obj);
+ printf("[getreadlock, %d] getting read lock for object %d: %d error: \n", numofcore, (int)ptr, rc_obj, strerror(rc_obj));
+ if(EBUSY == rc_obj) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+#endif
+}
+
+void releasereadlock(void * ptr) {
+#ifdef RAW
+
+#elif defined THREADSIMULATE
+ int numofcore = pthread_getspecific(key);
+ int rc = pthread_rwlock_rdlock(&rwlock_tbl);
+ printf("[releasereadlock, %d] getting the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ if(!RuntimeHashcontainskey(locktbl, (int)ptr)) {
+ printf("[releasereadlock, %d] Error: try to release a lock without previously grab it\n", numofcore);
+ exit(-1);
+ }
+ pthread_rwlock_t* rwlock_obj = NULL;
+ RuntimeHashget(locktbl, (int)ptr, (int*)&rwlock_obj);
+ int rc_obj = pthread_rwlock_unlock(rwlock_obj);
+ printf("[releasereadlock, %d] unlocked object %d: %d error: \n", numofcore, (int)ptr, rc_obj, strerror(rc_obj));
+ rc = pthread_rwlock_unlock(&rwlock_tbl);
+ printf("[releasereadlock, %d] release the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+#endif
+}
+
+bool getwritelock(void * ptr) {
+#ifdef RAW
+
+#elif defined THREADSIMULATE
+ int numofcore = pthread_getspecific(key);
+
+ int rc = pthread_rwlock_tryrdlock(&rwlock_tbl);
+ printf("[getwritelock, %d] getting the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ if(EBUSY == rc) {
+ return false;
+ }
+ if(!RuntimeHashcontainskey(locktbl, (int)ptr)) {
+ // no locks for this object
+ // first time to operate on this shared object
+ // create a lock for it
+ rc = pthread_rwlock_unlock(&rwlock_tbl);
+ printf("[getwritelock, %d] release the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ pthread_rwlock_t* rwlock = (pthread_rwlock_t *)RUNMALLOC(sizeof(pthread_rwlock_t));
+ memcpy(rwlock, &rwlock_init, sizeof(pthread_rwlock_t));
+ rc = pthread_rwlock_init(rwlock, NULL);
+ printf("[getwritelock, %d] initialize the rwlock for object %d: %d error: \n", numofcore, (int)ptr, rc, strerror(rc));
+ rc = pthread_rwlock_trywrlock(&rwlock_tbl);
+ printf("[getwritelock, %d] getting the write lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ if(EBUSY == rc) {
+ return false;
+ } else {
+ RuntimeHashadd(locktbl, (int)ptr, (int)rwlock);
+ rc = pthread_rwlock_unlock(&rwlock_tbl);
+ printf("[getwritelock, %d] release the write lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ }
+ //rc = pthread_rwlock_wrlock(rwlock);
+ rc = pthread_rwlock_trywrlock(rwlock);
+ printf("[getwritelock, %d] getting write lock for object %d: %d error: \n", numofcore, (int)ptr, rc, strerror(rc));
+ if(EBUSY == rc) {
+ return false;
+ } else {
+ return true;
+ }
+ } else {
+ pthread_rwlock_t* rwlock_obj = NULL;
+ RuntimeHashget(locktbl, (int)ptr, (int*)&rwlock_obj);
+ rc = pthread_rwlock_unlock(&rwlock_tbl);
+ printf("[getwritelock, %d] release the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ //int rc_obj = pthread_rwlock_wrlock(rwlock_obj);
+ int rc_obj = pthread_rwlock_trywrlock(rwlock_obj);
+ printf("[getwritelock, %d] getting write lock for object %d: %d error: \n", numofcore, (int)ptr, rc_obj, strerror(rc_obj));
+ if(EBUSY == rc_obj) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+#endif
+}
+
+void releasewritelock(void * ptr) {
+#ifdef RAW
+
+#elif defined THREADSIMULATE
+ int numofcore = pthread_getspecific(key);
+ int rc = pthread_rwlock_rdlock(&rwlock_tbl);
+ printf("[releasewritelock, %d] getting the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+ if(!RuntimeHashcontainskey(locktbl, (int)ptr)) {
+ printf("[releasewritelock, %d] Error: try to release a lock without previously grab it\n", numofcore);
+ exit(-1);
+ }
+ pthread_rwlock_t* rwlock_obj = NULL;
+ RuntimeHashget(locktbl, (int)ptr, (int*)&rwlock_obj);
+ int rc_obj = pthread_rwlock_unlock(rwlock_obj);
+ printf("[releasewritelock, %d] unlocked object %d: %d error:\n", numofcore, (int)ptr, rc_obj, strerror(rc_obj));
+ rc = pthread_rwlock_unlock(&rwlock_tbl);
+ printf("[releasewritelock, %d] release the read lock for locktbl: %d error: \n", numofcore, rc, strerror(rc));
+#endif
+}
+