int cp_heap_lock(cp_heap *heap) { int rc; if ((heap->mode & COLLECTION_MODE_NOSYNC)) return EINVAL; if ((rc = cp_mutex_lock(heap->lock))) return rc; heap->txowner = cp_thread_self(); heap->mode |= COLLECTION_MODE_IN_TRANSACTION; return 0; }
/* lock and set the transaction indicators */ int cp_avltree_lock(cp_avltree *tree, int type) { int rc; if ((tree->mode & COLLECTION_MODE_NOSYNC)) return EINVAL; if ((rc = cp_avltree_lock_internal(tree, type))) return rc; tree->txtype = type; tree->transaction_owner = cp_thread_self(); tree->mode |= COLLECTION_MODE_IN_TRANSACTION; return 0; }
int cp_heap_unlock(cp_heap *heap) { cp_thread self = cp_thread_self(); if (heap->txowner == self) { heap->txowner = 0; heap->mode ^= COLLECTION_MODE_IN_TRANSACTION; } return cp_mutex_unlock(heap->lock); }
int cp_heap_txunlock(cp_heap *heap) { if (heap->mode & COLLECTION_MODE_NOSYNC) return 0; if (heap->mode & COLLECTION_MODE_IN_TRANSACTION) { cp_thread self = cp_thread_self(); if (cp_thread_equal(self, heap->txowner)) return 0; } return cp_mutex_unlock(heap->lock); }
int cp_avltree_txunlock(cp_avltree *tree) { if (tree->mode & COLLECTION_MODE_NOSYNC) return 0; if (tree->mode & COLLECTION_MODE_IN_TRANSACTION && tree->txtype == COLLECTION_LOCK_WRITE) { cp_thread self = cp_thread_self(); if (cp_thread_equal(self, tree->transaction_owner)) return 0; } return cp_avltree_unlock_internal(tree); }
/* unset the transaction indicators and unlock */ int cp_avltree_unlock(cp_avltree *tree) { cp_thread self = cp_thread_self(); if (tree->transaction_owner == self) { tree->transaction_owner = 0; tree->txtype = 0; tree->mode ^= COLLECTION_MODE_IN_TRANSACTION; } else if (tree->txtype == COLLECTION_LOCK_WRITE) return CP_UNLOCK_FAILED; return cp_avltree_unlock_internal(tree); }
int cp_log_init(char *filename, int verbosity) { int i, err_code_count; unsigned long thread_no; #ifdef SIGHUP struct sigaction act; #endif log_filename = strdup(filename); logfile = fopen(filename, "a+"); if (logfile == NULL) return CP_LOG_FILE_OPEN_FAILURE; loglevel = verbosity; err_code_count = sizeof(error_messages) / sizeof(error_code_legend); error_message_lookup = cp_hashtable_create_by_mode(COLLECTION_MODE_NOSYNC, err_code_count * 2, cp_hash_int, cp_hash_compare_int); for (i = 0; i < err_code_count; i++) { error_code_legend *entry = &error_messages[i]; cp_hashtable_put(error_message_lookup, &entry->code, entry->msg); } thread_id = cp_hashtable_create_by_option(COLLECTION_MODE_DEEP, 10, cp_hash_long, cp_hash_compare_long, NULL, free, NULL, free); thread_count = 0; // thread_no = (unsigned long) pthread_self(); thread_no = (unsigned long) cp_thread_self(); get_thread_serial(thread_no); /* establish this as thread number one */ log_closing = 0; #ifdef SIGHUP act.sa_handler = cp_log_default_signal_handler; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGHUP, &act, NULL); #endif // signal(SIGHUP, cp_log_default_signal_handler); return 0; }
int cp_avltree_txlock(cp_avltree *tree, int type) { /* clear errno to allow client code to distinguish between a NULL return * value indicating the tree doesn't contain the requested value and NULL * on locking failure in tree operations */ errno = 0; if (tree->mode & COLLECTION_MODE_NOSYNC) return 0; if (tree->mode & COLLECTION_MODE_IN_TRANSACTION && tree->txtype == COLLECTION_LOCK_WRITE) { cp_thread self = cp_thread_self(); if (cp_thread_equal(self, tree->transaction_owner)) return 0; } return cp_avltree_lock_internal(tree, type); }
static void cc_printout(char *type, char *fmt, va_list argp) { char buf[MAX_LOG_MESSAGE_LEN]; struct tm *local; char timestr[256]; unsigned long tid; unsigned long thread_no; #ifdef PRECISE_TIME struct timeval now; gettimeofday(&now, NULL); local = localtime(&now.tv_sec); #else //precise time_t now; time(&now); local = localtime(&now); #endif //precise // thread_no = (unsigned long) pthread_self(); // tid = pthread_self(); tid = (unsigned long) cp_thread_self(); thread_no = get_thread_serial(tid); strftime(timestr, 256, cc_time_format, local); #ifdef CP_HAS_VSNPRINTF vsnprintf(buf, MAX_LOG_MESSAGE_LEN, fmt, argp); #else vsprintf(buf, fmt, argp); #endif /* CP_HAS_VSNPRINTF */ #ifdef PRECISE_TIME printf("%s" PRECISE_TIME_FORMAT " [%lX] %s: %s\n", timestr, now.tv_usec / PRECISE_TIME_FACTOR, thread_no, type, buf); if (logfile) fprintf(logfile, "%s" PRECISE_TIME_FORMAT " [%lX] %s: %s\n", timestr, now.tv_usec / PRECISE_TIME_FACTOR, thread_no, type, buf); #else /* precise */ printf("%s [%lu] %s: %s\n", timestr, thread_no, type, buf); if (logfile) fprintf(logfile, "%s [%lu] %s: %s\n", timestr, thread_no, type, buf); #endif /* precise */ if (logfile) fflush(logfile); }