Skip to content

Commit

Permalink
cleanup: os_thread_create -> std::thread
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgs committed Mar 19, 2021
1 parent da34288 commit dbe941e
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 182 deletions.
13 changes: 3 additions & 10 deletions extra/mariabackup/backup_copy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ struct datadir_thread_ctxt_t {
uint n_thread;
uint *count;
pthread_mutex_t* count_mutex;
os_thread_id_t id;
bool ret;
};

Expand Down Expand Up @@ -947,7 +946,7 @@ backup_file_printf(const char *filename, const char *fmt, ...)

static
bool
run_data_threads(datadir_iter_t *it, os_thread_func_t func, uint n)
run_data_threads(datadir_iter_t *it, void (*func)(datadir_thread_ctxt_t *ctxt), uint n)
{
datadir_thread_ctxt_t *data_threads;
uint i, count;
Expand All @@ -965,7 +964,7 @@ run_data_threads(datadir_iter_t *it, os_thread_func_t func, uint n)
data_threads[i].n_thread = i + 1;
data_threads[i].count = &count;
data_threads[i].count_mutex = &count_mutex;
data_threads[i].id = os_thread_create(func, data_threads + i);
std::thread(func, data_threads + i).detach();
}

/* Wait for threads to exit */
Expand Down Expand Up @@ -2036,13 +2035,10 @@ decrypt_decompress_file(const char *filepath, uint thread_n)
return(true);
}

static
os_thread_ret_t STDCALL
decrypt_decompress_thread_func(void *arg)
static void decrypt_decompress_thread_func(datadir_thread_ctxt_t *ctxt)
{
bool ret = true;
datadir_node_t node;
datadir_thread_ctxt_t *ctxt = (datadir_thread_ctxt_t *)(arg);

datadir_node_init(&node);

Expand Down Expand Up @@ -2072,9 +2068,6 @@ decrypt_decompress_thread_func(void *arg)
pthread_mutex_unlock(ctxt->count_mutex);

ctxt->ret = ret;

os_thread_exit();
OS_THREAD_DUMMY_RETURN;
}

bool
Expand Down
7 changes: 2 additions & 5 deletions extra/mariabackup/backup_mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ wait_for_no_updates(MYSQL *connection, uint timeout, uint threshold)
return(false);
}

static os_thread_ret_t DECLARE_THREAD(kill_query_thread)(void*)
static void kill_query_thread()
{
mysql_mutex_lock(&kill_query_thread_mutex);

Expand Down Expand Up @@ -835,9 +835,6 @@ static os_thread_ret_t DECLARE_THREAD(kill_query_thread)(void*)
kill_query_thread_running= false;
mysql_cond_signal(&kill_query_thread_stopped);
mysql_mutex_unlock(&kill_query_thread_mutex);

os_thread_exit();
OS_THREAD_DUMMY_RETURN;
}


Expand All @@ -849,7 +846,7 @@ static void start_query_killer()
mysql_mutex_init(0, &kill_query_thread_mutex, nullptr);
mysql_cond_init(0, &kill_query_thread_stop, nullptr);
mysql_cond_init(0, &kill_query_thread_stopped, nullptr);
os_thread_create(kill_query_thread);
std::thread(kill_query_thread).detach();
}

static void stop_query_killer()
Expand Down
22 changes: 5 additions & 17 deletions extra/mariabackup/xtrabackup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,6 @@ typedef struct {
uint num;
uint *count;
pthread_mutex_t* count_mutex;
os_thread_id_t id;
CorruptedPages *corrupted_pages;
} data_thread_ctxt_t;

Expand Down Expand Up @@ -3013,7 +3012,7 @@ void backup_wait_for_lsn(lsn_t lsn) {

extern lsn_t server_lsn_after_lock;

static os_thread_ret_t DECLARE_THREAD(log_copying_thread)(void*)
static void log_copying_thread()
{
my_thread_init();
mysql_mutex_lock(&log_sys.mutex);
Expand All @@ -3027,8 +3026,6 @@ static os_thread_ret_t DECLARE_THREAD(log_copying_thread)(void*)
log_copying_running= false;
mysql_mutex_unlock(&log_sys.mutex);
my_thread_end();
os_thread_exit();
return 0;
}

static bool have_io_watching_thread;
Expand Down Expand Up @@ -3096,15 +3093,9 @@ void dbug_mariabackup_event(const char *event,const char *key)
}
#endif // DBUG_OFF

/**************************************************************************
Datafiles copying thread.*/
static
os_thread_ret_t
DECLARE_THREAD(data_copy_thread_func)(
/*==================*/
void *arg) /* thread context */
/** Datafiles copying thread.*/
static void data_copy_thread_func(data_thread_ctxt_t *ctxt) /* thread context */
{
data_thread_ctxt_t *ctxt = (data_thread_ctxt_t *) arg;
uint num = ctxt->num;
fil_node_t* node;
ut_ad(ctxt->corrupted_pages);
Expand Down Expand Up @@ -3136,8 +3127,6 @@ DECLARE_THREAD(data_copy_thread_func)(
pthread_mutex_unlock(ctxt->count_mutex);

my_thread_end();
os_thread_exit();
OS_THREAD_DUMMY_RETURN;
}

/************************************************************************
Expand Down Expand Up @@ -4424,7 +4413,7 @@ static bool xtrabackup_backup_func()
DBUG_MARIABACKUP_EVENT("before_innodb_log_copy_thread_started",0);

mysql_cond_init(0, &log_copying_stop, nullptr);
os_thread_create(log_copying_thread);
std::thread(log_copying_thread).detach();

/* FLUSH CHANGED_PAGE_BITMAPS call */
if (!flush_changed_page_bitmaps()) {
Expand Down Expand Up @@ -4466,8 +4455,7 @@ static bool xtrabackup_backup_func()
data_threads[i].count = &count;
data_threads[i].count_mutex = &count_mutex;
data_threads[i].corrupted_pages = &corrupted_pages;
data_threads[i].id = os_thread_create(data_copy_thread_func,
data_threads + i);
std::thread(data_copy_thread_func, data_threads + i).detach();
}

/* Wait for threads to exit */
Expand Down
17 changes: 7 additions & 10 deletions storage/innobase/buf/buf0flu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2025,11 +2025,9 @@ static ulint page_cleaner_flush_pages_recommendation(ulint last_pages_in,
return(n_pages);
}

/******************************************************************//**
page_cleaner thread tasked with flushing dirty pages from the buffer
pools. As of now we'll have only one coordinator.
@return a dummy parameter */
static os_thread_ret_t DECLARE_THREAD(buf_flush_page_cleaner)(void*)
/** page_cleaner thread tasked with flushing dirty pages from the buffer
pools. As of now we'll have only one coordinator. */
static void buf_flush_page_cleaner()
{
my_thread_init();
#ifdef UNIV_PFS_THREAD
Expand Down Expand Up @@ -2253,11 +2251,10 @@ static os_thread_ret_t DECLARE_THREAD(buf_flush_page_cleaner)(void*)
mysql_mutex_unlock(&buf_pool.flush_list_mutex);

my_thread_end();
/* We count the number of threads in os_thread_exit(). A created
thread should always use that to exit and not use return() to exit. */
os_thread_exit();

OS_THREAD_DUMMY_RETURN;
#ifdef UNIV_PFS_THREAD
pfs_delete_thread();
#endif
}

/** Initialize page_cleaner. */
Expand All @@ -2269,7 +2266,7 @@ ATTRIBUTE_COLD void buf_flush_page_cleaner_init()
srv_operation == SRV_OPERATION_RESTORE_EXPORT);
buf_flush_sync_lsn= 0;
buf_page_cleaner_is_active= true;
os_thread_create(buf_flush_page_cleaner);
std::thread(buf_flush_page_cleaner).detach();
}

/** @return the number of dirty pages in the buffer pool */
Expand Down
20 changes: 9 additions & 11 deletions storage/innobase/fil/fil0crypt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2062,10 +2062,9 @@ static void fil_crypt_complete_rotate_space(rotate_thread_t* state)
mysql_mutex_unlock(&crypt_data->mutex);
}

/*********************************************************************//**
A thread which monitors global key state and rotates tablespaces accordingly
@return a dummy parameter */
static os_thread_ret_t DECLARE_THREAD(fil_crypt_thread)(void*)
/** A thread which monitors global key state and rotates tablespaces
accordingly */
static void fil_crypt_thread()
{
mysql_mutex_lock(&fil_crypt_threads_mutex);
rotate_thread_t thr(srv_n_fil_crypt_threads_started++);
Expand Down Expand Up @@ -2144,12 +2143,9 @@ static os_thread_ret_t DECLARE_THREAD(fil_crypt_thread)(void*)
pthread_cond_signal(&fil_crypt_cond); /* signal that we stopped */
mysql_mutex_unlock(&fil_crypt_threads_mutex);

/* We count the number of threads in os_thread_exit(). A created
thread should always use that to exit and not use return() to exit. */

os_thread_exit();

OS_THREAD_DUMMY_RETURN;
#ifdef UNIV_PFS_THREAD
pfs_delete_thread();
#endif
}

/*********************************************************************
Expand All @@ -2172,10 +2168,12 @@ fil_crypt_set_thread_cnt(
uint add = new_cnt - srv_n_fil_crypt_threads;
srv_n_fil_crypt_threads = new_cnt;
for (uint i = 0; i < add; i++) {
std::thread thd(fil_crypt_thread);
ib::info() << "Creating #"
<< i+1 << " encryption thread id "
<< os_thread_create(fil_crypt_thread)
<< thd.get_id()
<< " total threads " << new_cnt << ".";
thd.detach();
}
} else if (new_cnt < srv_n_fil_crypt_threads) {
srv_n_fil_crypt_threads = new_cnt;
Expand Down
35 changes: 1 addition & 34 deletions storage/innobase/include/os0thread.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*****************************************************************************
Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2017, 2020, MariaDB Corporation.
Copyright (c) 2017, 2021, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Expand All @@ -28,48 +28,15 @@ Created 9/8/1995 Heikki Tuuri
#pragma once
#include "univ.i"

/* Possible fixed priorities for threads */
#define OS_THREAD_PRIORITY_NONE 100
#define OS_THREAD_PRIORITY_BACKGROUND 1
#define OS_THREAD_PRIORITY_NORMAL 2
#define OS_THREAD_PRIORITY_ABOVE_NORMAL 3

#ifdef _WIN32
typedef DWORD os_thread_t;
typedef DWORD os_thread_id_t; /*!< In Windows the thread id
is an unsigned long int */
extern "C" {
typedef LPTHREAD_START_ROUTINE os_thread_func_t;
}

/** Macro for specifying a Windows thread start function. */
#define DECLARE_THREAD(func) WINAPI func
#else

typedef pthread_t os_thread_t;
typedef pthread_t os_thread_id_t; /*!< In Unix we use the thread
handle itself as the id of
the thread */
extern "C" { typedef void* (*os_thread_func_t)(void*); }

/** Macro for specifying a POSIX thread start function. */
#define DECLARE_THREAD(func) func
#endif /* _WIN32 */

/* Define a function pointer type to use in a typecast */
typedef void* (*os_posix_f_t) (void*);

#define os_thread_eq(a,b) IF_WIN(a == b, pthread_equal(a, b))
#define os_thread_get_curr_id() IF_WIN(GetCurrentThreadId(), pthread_self())

/****************************************************************//**
Creates a new thread of execution. The execution starts from
the function given.
NOTE: We count the number of threads in os_thread_exit(). A created
thread should always use that to exit so thatthe thread count will be
decremented.
We do not return an error code because if there is one, we crash here. */
os_thread_t os_thread_create(os_thread_func_t func, void *arg= nullptr);

/** Detach and terminate the current thread. */
ATTRIBUTE_NORETURN void os_thread_exit();
7 changes: 2 additions & 5 deletions storage/innobase/include/trx0roll.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ Rollback or clean up any incomplete transactions which were
encountered in crash recovery. If the transaction already was
committed, then we clean up a possible insert undo log. If the
transaction was not yet committed, then we roll it back.
Note: this is done in a background thread.
@return a dummy parameter */
extern "C"
os_thread_ret_t
DECLARE_THREAD(trx_rollback_all_recovered)(void*);
Note: this is done in a background thread. */
void trx_rollback_all_recovered(void*);
/*********************************************************************//**
Creates a rollback command node struct.
@return own: rollback node struct */
Expand Down
71 changes: 0 additions & 71 deletions storage/innobase/os/os0thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,74 +26,3 @@ Created 9/8/1995 Heikki Tuuri

#include "univ.i"
#include "srv0srv.h"

/****************************************************************//**
Creates a new thread of execution. The execution starts from
the function given.
NOTE: We count the number of threads in os_thread_exit(). A created
thread should always use that to exit so thatthe thread count will be
decremented.
We do not return an error code because if there is one, we crash here. */
os_thread_t os_thread_create(os_thread_func_t func, void *arg)
{
os_thread_id_t new_thread_id;

#ifdef _WIN32
HANDLE handle;

handle = CreateThread(NULL, /* no security attributes */
0, /* default size stack */
func,
arg,
0, /* thread runs immediately */
&new_thread_id);

if (!handle) {
/* If we cannot start a new thread, life has no meaning. */
ib::fatal() << "CreateThread returned " << GetLastError();
}

CloseHandle(handle);

return((os_thread_t)new_thread_id);
#else /* _WIN32 else */

pthread_attr_t attr;

int ret = pthread_attr_init(&attr);
if (UNIV_UNLIKELY(ret)) {
fprintf(stderr,
"InnoDB: Error: pthread_attr_init() returned %d\n",
ret);
abort();
}

ret = pthread_create(&new_thread_id, &attr, func, arg);

ut_a(ret == 0);

pthread_attr_destroy(&attr);

#endif /* not _WIN32 */

return((os_thread_t)new_thread_id);
}

/** Detach and terminate the current thread. */
ATTRIBUTE_NORETURN void os_thread_exit()
{
#ifdef UNIV_DEBUG_THREAD_CREATION
ib::info() << "Thread exits, id " << os_thread_get_curr_id();
#endif

#ifdef UNIV_PFS_THREAD
pfs_delete_thread();
#endif

#ifdef _WIN32
ExitThread(0);
#else
pthread_detach(pthread_self());
pthread_exit(NULL);
#endif
}
Loading

0 comments on commit dbe941e

Please sign in to comment.