/* * Test lock and write_and_unlock */ static gboolean inc_counter(file_lock *lock) { char old_val = 'a'; char new_val; if (lock->len) { old_val = lock->data[0]; } g_assert(old_val < 'z'); new_val = old_val + 1; if (file_lock_write(lock, &new_val, 1) == -1) { g_fprintf(stderr, "file_lock_write: %s\n", strerror(errno)); return FALSE; } return TRUE; }
// we already have the lock // remove the lock void write_cmdfile( cmddatas_t *cmddatas) { GPtrArray *lines = g_ptr_array_sized_new(100); char *buffer; // generate g_ptr_array_add(lines, g_strdup_printf("VERSION %d\n", cmddatas->version)); g_ptr_array_add(lines, g_strdup_printf("ID %d\n", cmddatas->max_id)); g_hash_table_foreach(cmddatas->cmdfile, &cmdfile_write, lines); g_ptr_array_add(lines, NULL); buffer = g_strjoinv(NULL, (gchar **)lines->pdata); g_ptr_array_free_full(lines); // write file_lock_write(cmddatas->lock, buffer, strlen(buffer)); g_free(buffer); // unlock file_lock_unlock(cmddatas->lock); }