From 76958bdbc3644af37431bd74ea4164a2e056ce5e Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Wed, 31 Jan 2018 22:57:49 +0100 Subject: [PATCH] Removed thread_pool implementation from libecl --- lib/CMakeLists.txt | 14 - lib/include/ert/util/thread_pool.h | 43 -- lib/util/tests/ert_util_spawn.c | 13 - lib/util/thread_pool.c | 27 -- lib/util/thread_pool1.c | 90 ---- lib/util/thread_pool_posix.c | 485 ------------------- python/ecl/util/util/CMakeLists.txt | 1 - python/ecl/util/util/__init__.py | 1 - python/ert/util/__init__.py | 1 - python/tests/legacy_tests/test_util.py | 6 +- python/tests/util_tests/CMakeLists.txt | 2 - python/tests/util_tests/test_cthread_pool.py | 48 -- 12 files changed, 5 insertions(+), 726 deletions(-) delete mode 100644 lib/include/ert/util/thread_pool.h delete mode 100644 lib/util/thread_pool.c delete mode 100644 lib/util/thread_pool1.c delete mode 100644 lib/util/thread_pool_posix.c delete mode 100644 python/tests/util_tests/test_cthread_pool.py diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 0e6f9ad740..8f0a0c9c2b 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,10 +1,5 @@ project(libecl-ecl C CXX) -if (HAVE_PTHREAD) - # The block_fs filesystem is so heavily dependant on pthreads that it is - # not built if de not have pthreads. - list(APPEND opt_srcs util/thread_pool.c) -endif () if (HAVE_BACKTRACE) @@ -275,15 +270,6 @@ add_test(NAME ert_util_work_area WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/util/tests ) -find_library( VALGRIND NAMES valgr ) -if (VALGRIND) - set(valgrind_cmd valgrind --error-exitcode=1 --tool=memcheck) -endif () - -add_executable(test_thread_pool util/tests/test_thread_pool.c) -target_link_libraries(test_thread_pool ecl) -add_test(NAME test_thread_pool COMMAND ${valgrind_cmd} test_thread_pool) - add_executable(ert_util_cwd_test util/tests/ert_util_cwd_test.c) target_link_libraries(ert_util_cwd_test ecl) add_test(NAME ert_util_cwd_test COMMAND ert_util_cwd_test ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/lib/include/ert/util/thread_pool.h b/lib/include/ert/util/thread_pool.h deleted file mode 100644 index 23abb308a2..0000000000 --- a/lib/include/ert/util/thread_pool.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - Copyright (C) 2011 Statoil ASA, Norway. - - The file 'thread_pool.h' is part of ERT - Ensemble based Reservoir Tool. - - ERT 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 Foundation, either version 3 of the License, or - (at your option) any later version. - - ERT is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. - - See the GNU General Public License at - for more details. -*/ -#ifndef ERT_THREAD_POOL_H -#define ERT_THREAD_POOL_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - - typedef struct thread_pool_struct thread_pool_type; - - void thread_pool_join(thread_pool_type * ); - thread_pool_type * thread_pool_alloc(int , bool start_queue); - void thread_pool_add_job(thread_pool_type * ,void * (*) (void *) , void *); - void thread_pool_free(thread_pool_type *); - void thread_pool_restart( thread_pool_type * tp ); - void * thread_pool_iget_return_value( const thread_pool_type * pool , int queue_index ); - int thread_pool_get_max_running( const thread_pool_type * pool ); - bool thread_pool_try_join(thread_pool_type * pool, int timeout_seconds); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/lib/util/tests/ert_util_spawn.c b/lib/util/tests/ert_util_spawn.c index 17535d4d2b..0a81e4194f 100644 --- a/lib/util/tests/ert_util_spawn.c +++ b/lib/util/tests/ert_util_spawn.c @@ -25,7 +25,6 @@ #include #include #include -#include static const char * stdout_msg = "stdout_xxx"; @@ -178,18 +177,6 @@ void test_spawn_redirect_threaded() { test_assert_true(check_script(script)); } - // Run the scripts in parallel - stringlist_type * script_paths = stringlist_alloc_new(); // free the paths after threads have completed - thread_pool_type * tp = thread_pool_alloc( 8 , true ); - for(int i = 0; i < num; i++) { - char * path = util_alloc_sprintf("%06d" , path_codes[i]); - stringlist_append_owned_ref(script_paths, path); - thread_pool_add_job( tp , test_spawn_redirect__ , path ); - } - thread_pool_join( tp ); - thread_pool_free( tp ); - stringlist_free(script_paths); - stringlist_free(script_fullpaths); util_free(path_codes); test_work_area_free( test_area ); diff --git a/lib/util/thread_pool.c b/lib/util/thread_pool.c deleted file mode 100644 index e23026b4ea..0000000000 --- a/lib/util/thread_pool.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - Copyright (C) 2011 Statoil ASA, Norway. - - The file 'thread_pool.c' is part of ERT - Ensemble based Reservoir Tool. - - ERT 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 Foundation, either version 3 of the License, or - (at your option) any later version. - - ERT is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. - - See the GNU General Public License at - for more details. -*/ - -#include "ert/util/build_config.h" -#include - -#ifdef HAVE_PTHREAD -#include "thread_pool_posix.c" -#else -Error - should not be be here. -#endif - diff --git a/lib/util/thread_pool1.c b/lib/util/thread_pool1.c deleted file mode 100644 index d365a67e30..0000000000 --- a/lib/util/thread_pool1.c +++ /dev/null @@ -1,90 +0,0 @@ -/* - Copyright (C) 2011 Statoil ASA, Norway. - - The file 'thread_pool1.c' is part of ERT - Ensemble based Reservoir Tool. - - ERT 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 Foundation, either version 3 of the License, or - (at your option) any later version. - - ERT is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. - - See the GNU General Public License at - for more details. -*/ - -#include -#include -#include -#include -#include - -struct thread_pool_struct { - int pool_size; - int jobs_running; - pthread_t *thread_list; -}; - - - - - -static void thread_pool_resize(thread_pool_type * pool, int new_size) { - pool->pool_size = new_size; - pool->thread_list = realloc(pool->thread_list , new_size * sizeof * pool->thread_list); -} - - - -void thread_pool_join(thread_pool_type * pool) { - int i; - if (pool->pool_size == 0) - return; - else { - for (i=0; i < pool->jobs_running; i++) - pthread_join(pool->thread_list[i] , NULL); /* Second argument: void **value_ptr */ - pool->jobs_running = 0; - } -} - - -thread_pool_type * thread_pool_alloc(int pool_size) { - thread_pool_type * pool = util_malloc(sizeof *pool); - pool->thread_list = NULL; - thread_pool_resize(pool , pool_size); - pool->jobs_running = 0; - return pool; -} - - - -void thread_pool_add_job(thread_pool_type * pool , - void * (start_func) (void *) , void *arg) { - - if (pool->pool_size == 0) - start_func(arg); - else { - - if (pool->jobs_running == pool->pool_size) - thread_pool_join(pool); - - { - int pthread_return = pthread_create( &pool->thread_list[pool->jobs_running] , NULL , start_func , arg); - if (pthread_return != 0) - util_abort("%s: failed to add new job pthread_create return value: %d.\n",__func__ , pthread_return); - } - - pool->jobs_running++; - } - -} - -void thread_pool_free(thread_pool_type * pool) { - if (pool->thread_list != NULL) free(pool->thread_list); - free(pool); -} - - diff --git a/lib/util/thread_pool_posix.c b/lib/util/thread_pool_posix.c deleted file mode 100644 index b8e188ef15..0000000000 --- a/lib/util/thread_pool_posix.c +++ /dev/null @@ -1,485 +0,0 @@ -/* - Copyright (C) 2011 Statoil ASA, Norway. - - The file 'thread_pool_posix.c' is part of ERT - Ensemble based Reservoir Tool. - - ERT 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 Foundation, either version 3 of the License, or - (at your option) any later version. - - ERT is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. - - See the GNU General Public License at - for more details. -*/ - -#define _GNU_SOURCE /* Must define this to get access to pthread_rwlock_t */ -#include -#include -#include -#include -#include -#include - -#include "ert/util/build_config.h" - -#include -#include -#include - - -/** - This file implements a small thread_pool object based on - pthread_create() function calls. The characetristics of this - implementation is as follows: - - 1. The jobs are mangaged by a separate thread - dispatch_thread. - 2. The new jobs are just appended to the queue, the - dispatch_thread sees them in the queue and dispatches them. - 3. The dispatch thread manages a list of thread_pool_job_slot_type - instances - one slot for each actually running job. - - Example - ------- - - 1. Start with creating a thread pool object. The arguments to the - allocater are the (maximum) number of concurrently running - threads and a boolean flag of whether the queue should start - immediately (that is in general the case). - - thread_pool_type * tp = thread_pool_alloc( NUM_THREADS , immediate_start); - - - 2. Add the jobs you want to run: - - thread_pool_add_job( tp , some_function , argument_to_some_function ); - - Here the prototype for the function which is being run is - - void * (some_func) (void *); - - I.e. it expects a (void *) input pointer, and also returns a - (void *) pointer as output. The thread pool implementation does - not touch the input and output of some_function. - - - 3. When all the jobs have been added you inform the thread pool of - that by calling: - - thread_pool_join( tp ); - - This function will not return before all the added jobs have run - to completion. - - - 4. Optional: If you want to get the return value from the function - you supplied, you can use: - - thread_pool_iget_return_value( tp , index ); - - To get the return value from function nr index. - - - 5. Optional: The thread pool will probably mainly be used only once, - but after a join it is possible to reuse a thread pool, but then - you MUST call thread_pool_restart() before adding jobs again. - - - 6. When you are really finished: thread_pool_free( tp ); - -*/ - - -typedef void * (start_func_ftype) (void *) ; - - -/** - Internal struct which is used as queue node. -*/ -typedef struct { - thread_pool_type * pool; /* A back-reference to the thread_pool holding the queue. */ - int slot_index; /* The index in the space [0,max_running) of the job slot where this job is running. */ - int queue_index; /* The index of the current tp_arg in the queue. */ - void * func_arg; /* The arguments to this job - supplied by the calling scope. */ - start_func_ftype * func; /* The function to call - supplied by the calling scope. */ - void * return_value; -} thread_pool_arg_type; - - - -/** - Internal struct used to keep track of the job slots. -*/ -typedef struct { - pthread_t thread; /* The thread variable currently (or more correct:last) running. */ - int run_count; /* The number of times this slot has been used - just to check whether the slot has been used AT ALL when/if joining. */ - bool running; /* Is the job_slot running now?? */ -} thread_pool_job_slot_type; - - - - -#define THREAD_POOL_TYPE_ID 71443207 -struct thread_pool_struct { - UTIL_TYPE_ID_DECLARATION; - thread_pool_arg_type * queue; /* The jobs to be executed are appended in this vector. */ - int queue_index; /* The index of the next job to run. */ - int queue_size; /* The number of jobs in the queue - including those which are complete. [Should be protected / atomic / ... ] */ - int queue_alloc_size; /* The allocated size of the queue. */ - - int max_running; /* The max number of concurrently running jobs. */ - bool join; /* Flag set by the main thread to inform the dispatch thread that joining should start. */ - bool accepting_jobs; /* True|False whether the dispatch thread is running. */ - - thread_pool_job_slot_type * job_slots; /* A vector to @max_running job slots, each slot can be reused several times.*/ - pthread_t dispatch_thread; - pthread_rwlock_t queue_lock; -}; - - -static UTIL_SAFE_CAST_FUNCTION( thread_pool , THREAD_POOL_TYPE_ID ) - - -/** - This function will grow the queue. It is called by the main thread - (i.e. the context of the calling scope), and the queue is read by - the dispatch_thread - i.e. access to the queue must be protected by - rwlock. -*/ - -static void thread_pool_resize_queue( thread_pool_type * pool, int queue_length ) { - pthread_rwlock_wrlock( &pool->queue_lock ); - { - pool->queue = (thread_pool_arg_type*)util_realloc( pool->queue , queue_length * sizeof * pool->queue ); - pool->queue_alloc_size = queue_length; - } - pthread_rwlock_unlock( &pool->queue_lock ); -} - - -/** - This function updates an element in the queue, the function is - called by the executing threads, on the same time the main thread - might be resizing the thread, we therefore take a read lock during - execution of this function. (Write lock is not necessary because we - will not change the queue pointer itself, only something it points - to.) -*/ - -static void thread_pool_iset_return_value( thread_pool_type * pool , int index , void * return_value) { - pthread_rwlock_rdlock( &pool->queue_lock ); - { - pool->queue[ index ].return_value = return_value; - } - pthread_rwlock_unlock( &pool->queue_lock ); -} - - -void * thread_pool_iget_return_value( const thread_pool_type * pool , int queue_index ) { - return pool->queue[ queue_index ].return_value; -} - - -/** - The pthread_create() call which this is all about, does not start - the user supplied function. Instead it will start an instance of - this function, which will do some housekeeping before calling the - user supplied function. -*/ - -static void * thread_pool_start_job( void * arg ) { - thread_pool_arg_type * tp_arg = (thread_pool_arg_type * ) arg; - thread_pool_type * tp = tp_arg->pool; - int slot_index = tp_arg->slot_index; - void * func_arg = tp_arg->func_arg; - start_func_ftype * func = tp_arg->func; - void * return_value; - - - return_value = func( func_arg ); /* Starting the real external function */ - tp->job_slots[ slot_index ].running = false; /* We mark the job as completed. */ - free( arg ); - - if (return_value != NULL) - thread_pool_iset_return_value( tp , tp_arg->queue_index , return_value); - - return NULL; -} - - - -/** - This function is run by the dispatch_thread. The thread will keep - an eye on the queue, and dispatch new jobs when there are free - slots available. -*/ - -static void * thread_pool_main_loop( void * arg ) { - thread_pool_type * tp = thread_pool_safe_cast( arg ); - { - const int usleep_init = 1000; /* The sleep time when there are free slots available - but no jobs wanting to run. */ - int internal_offset = 0; /* Keep track of the (index of) the last job slot fired off - minor time saving. */ - while (true) { - if (tp->queue_size > tp->queue_index) { - /* - There are jobs in the queue which would like to run - - let us see if we can find a slot for them. - */ - int counter = 0; - bool slot_found = false; - do { - int slot_index = (counter + internal_offset) % tp->max_running; - thread_pool_job_slot_type * job_slot = &tp->job_slots[ slot_index ]; - if (!job_slot->running) { - /* OK thread[slot_index] is ready to take this job.*/ - thread_pool_arg_type * tp_arg; - - /* - The queue might be updated by the main thread - we must - take a copy of the node we are interested in. - */ - pthread_rwlock_rdlock( &tp->queue_lock ); - tp_arg = (thread_pool_arg_type*)util_alloc_copy( &tp->queue[ tp->queue_index ] , sizeof * tp_arg ); - pthread_rwlock_unlock( &tp->queue_lock ); - - tp_arg->slot_index = slot_index; - job_slot->running = true; - /* - Here is the actual pthread_create() call creating an - additional running thread. - */ - - /*Cleanup of previous run threads. Needed to avoid memory leak*/ - if (job_slot->run_count > 0) - pthread_join(job_slot->thread, NULL); - - pthread_create( &job_slot->thread , NULL , thread_pool_start_job , tp_arg ); - job_slot->run_count += 1; - tp->queue_index++; - internal_offset += (counter + 1); - slot_found = true; - } else - counter++; - } while (!slot_found && (counter < tp->max_running)); - - if (!slot_found) { - util_yield(); - } - } else - util_usleep(usleep_init); /* There are no jobs wanting to run. */ - - /*****************************************************************/ - /* - We exit explicitly from this loop when both conditions apply: - - 1. tp->join == true : The calling scope has signaled that it will not submit more jobs. - 2. tp->queue_size == tp->queue_index : This function has submitted all the jobs in the queue. - */ - if ((tp->join) && (tp->queue_size == tp->queue_index)) - break; - } /* End of while() loop */ - } - - /* - There are no more jobs in the queue, and the main scope has - signaled that join should start. Observe that we join only the - jobs corresponding to explicitly running job_slots; when a job - slot is used multiple times the first jobs run in the job_slot - will not be explicitly joined. - */ - { - int i; - for (i=0; i < tp->max_running; i++) { - thread_pool_job_slot_type job_slot = tp->job_slots[i]; - if (job_slot.run_count > 0) - pthread_join( job_slot.thread , NULL ); - } - } - /* When we are here all the jobs have completed. */ - return NULL; -} - - - - -/** - This function initializes a couple of counters, and starts up the - dispatch thread. If the thread_pool should be reused after a join, - this function must be called before adding new jobs. - - The functions thread_pool_restart() and thread_pool_join() should - be joined up like open/close and malloc/free combinations. -*/ - -void thread_pool_restart( thread_pool_type * tp ) { - if (tp->accepting_jobs) - util_abort("%s: fatal error - tried restart already running thread pool\n",__func__); - { - tp->join = false; - tp->queue_index = 0; - tp->queue_size = 0; - { - int i; - for (i=0; i < tp->max_running; i++) { - tp->job_slots[i].run_count = 0; - tp->job_slots[i].running = false; - } - } - - /* Starting the dispatch thread. */ - pthread_create( &tp->dispatch_thread , NULL , thread_pool_main_loop , tp ); - tp->accepting_jobs = true; - } -} - - - -/** - This function is called by the calling scope when all the jobs have - been submitted, and we just wait for them to complete. - - This function just sets the join switch to true - this again tells - the dispatch_thread to start the join process on the worker - threads. -*/ - -void thread_pool_join(thread_pool_type * pool) { - pool->join = true; /* Signals to the main thread that joining can start. */ - if (pool->max_running > 0) { - pthread_join( pool->dispatch_thread , NULL ); /* Wait for the main thread to complete. */ - pool->accepting_jobs = false; - } -} - -/* - This will try to join the thread; if the manager thread has not - completed within @timeout_seconds the function will return false. If - the join fails the queue will be reset in a non-joining state and it - will be open for more jobs. Probably not in a 100% sane state. -*/ - -bool thread_pool_try_join(thread_pool_type * pool, int timeout_seconds) { - bool join_ok = true; - - pool->join = true; /* Signals to the main thread that joining can start. */ - if (pool->max_running > 0) { - time_t timeout_time = time( NULL ); - util_inplace_forward_seconds_utc(&timeout_time , timeout_seconds ); - -#ifdef HAVE_TIMEDJOIN - - struct timespec ts; - ts.tv_sec = timeout_time; - ts.tv_nsec = 0; - - { - int join_return = pthread_timedjoin_np( pool->dispatch_thread , NULL , &ts); /* Wait for the main thread to complete. */ - if (join_return == 0) - pool->accepting_jobs = false; - else { - pool->join = false; - join_ok = false; - } - } - -#else - - while(true) { - if (pthread_kill(pool->dispatch_thread, 0) == 0){ - util_yield(); - } else { - pthread_join(pool->dispatch_thread, NULL); - pool->accepting_jobs = false; - break; - } - - time_t now = time(NULL); - - if(util_difftime_seconds(now, timeout_time) <= 0) { - join_ok = false; - break; - } - } - -#endif - - - - } - return join_ok; -} - - - - -/** - max_running is the maximum number of concurrent threads. If - @start_queue is true the dispatch thread will start immediately. If - the function is called with @start_queue == false you must first - call thread_pool_restart() BEFORE you can start adding jobs. -*/ - -thread_pool_type * thread_pool_alloc(int max_running , bool start_queue) { - thread_pool_type * pool = (thread_pool_type*)util_malloc( sizeof *pool ); - UTIL_TYPE_ID_INIT( pool , THREAD_POOL_TYPE_ID ); - pool->job_slots = (thread_pool_job_slot_type*)util_calloc( max_running , sizeof * pool->job_slots ); - pool->max_running = max_running; - pool->queue = NULL; - pool->accepting_jobs = false; - pthread_rwlock_init( &pool->queue_lock , NULL); - thread_pool_resize_queue( pool , 32 ); - if (start_queue) - thread_pool_restart( pool ); - return pool; -} - - - -void thread_pool_add_job(thread_pool_type * pool , start_func_ftype * start_func , void * func_arg ) { - if (pool->max_running == 0) /* Blocking non-threaded mode: */ - start_func( func_arg ); - else { - if (pool->accepting_jobs) { - if (pool->queue_size == pool->queue_alloc_size) - thread_pool_resize_queue( pool , pool->queue_alloc_size * 2); - - /* - The new job is added to the queue - the main thread is watching - the queue and will pick up the new job. - */ - { - int queue_index = pool->queue_size; - - pool->queue[ queue_index ].pool = pool; - pool->queue[ queue_index ].func_arg = func_arg; - pool->queue[ queue_index ].func = start_func; - pool->queue[ queue_index ].return_value = NULL; - pool->queue[ queue_index ].queue_index = queue_index; - } - pool->queue_size++; /* <- This is shared between this thread and the dispatch thread */ - } else - util_abort("%s: thread_pool is not running - restart with thread_pool_restart()?? \n",__func__); - } -} - - - -/* - Observe that this function does not join the worker threads, - i.e. you should call thread_pool_join() first (otherwise the thing - will go up in flames). -*/ - - -void thread_pool_free(thread_pool_type * pool) { - util_safe_free( pool->job_slots ); - util_safe_free( pool->queue ); - free(pool); -} - -int thread_pool_get_max_running( const thread_pool_type * pool ) { - return pool->max_running; -} diff --git a/python/ecl/util/util/CMakeLists.txt b/python/ecl/util/util/CMakeLists.txt index a30470e397..8374950ce2 100644 --- a/python/ecl/util/util/CMakeLists.txt +++ b/python/ecl/util/util/CMakeLists.txt @@ -11,7 +11,6 @@ set(PYTHON_SOURCES stringlist.py #substitution_list.py thread_pool.py - cthread_pool.py time_vector.py util_func.py vector_template.py diff --git a/python/ecl/util/util/__init__.py b/python/ecl/util/util/__init__.py index ec6708e368..feccb678d1 100644 --- a/python/ecl/util/util/__init__.py +++ b/python/ecl/util/util/__init__.py @@ -61,7 +61,6 @@ from .lookup_table import LookupTable from .hash import Hash, StringHash, DoubleHash, IntegerHash from .thread_pool import ThreadPool -from .cthread_pool import CThreadPool, startCThreadPool from .install_abort_signals import installAbortSignals, updateAbortSignals from .arg_pack import ArgPack from .cwd_context import CWDContext diff --git a/python/ert/util/__init__.py b/python/ert/util/__init__.py index 45238d35f2..48e2d7bff7 100644 --- a/python/ert/util/__init__.py +++ b/python/ert/util/__init__.py @@ -12,7 +12,6 @@ from ecl.util.util import LookupTable from ecl.util.util import Hash, StringHash, DoubleHash, IntegerHash from ecl.util.util import ThreadPool -from ecl.util.util import CThreadPool, startCThreadPool from ecl.util.util import installAbortSignals, updateAbortSignals from ecl.util.util import ArgPack diff --git a/python/tests/legacy_tests/test_util.py b/python/tests/legacy_tests/test_util.py index 2310845c6d..a52dd8f442 100644 --- a/python/tests/legacy_tests/test_util.py +++ b/python/tests/legacy_tests/test_util.py @@ -12,7 +12,6 @@ from ert.util import LookupTable from ert.util import Hash, StringHash, DoubleHash, IntegerHash from ert.util import ThreadPool -from ert.util import CThreadPool, startCThreadPool from ert.util import installAbortSignals, updateAbortSignals from ert.util import ArgPack @@ -23,5 +22,10 @@ except ImportError: pass +try: + from res.util import CThreadPool, startCThreadPool +except ImportError: + pass + class ErtLegacyUtilTest(EclTest): pass diff --git a/python/tests/util_tests/CMakeLists.txt b/python/tests/util_tests/CMakeLists.txt index 59aec458a6..a14e586e39 100644 --- a/python/tests/util_tests/CMakeLists.txt +++ b/python/tests/util_tests/CMakeLists.txt @@ -7,7 +7,6 @@ set(TEST_SOURCES test_string_list.py #test_substitution_list.py test_thread_pool.py - test_cthread_pool.py test_vectors.py test_version.py test_work_area.py @@ -28,7 +27,6 @@ addPythonTest(tests.util_tests.test_work_area.WorkAreaTest) addPythonTest(tests.util_tests.test_version.VersionTest) addPythonTest(tests.util_tests.test_path_context.PathContextTest) addPythonTest(tests.util_tests.test_thread_pool.ThreadPoolTest) -addPythonTest(tests.util_tests.test_cthread_pool.CThreadPoolTest) addPythonTest(tests.util_tests.test_arg_pack.ArgPackTest) addPythonTest(tests.util_tests.test_spawn.SpawnTest) #addPythonTest(tests.util.test_substitution_list.SubstitutionListTest) diff --git a/python/tests/util_tests/test_cthread_pool.py b/python/tests/util_tests/test_cthread_pool.py deleted file mode 100644 index e31e661652..0000000000 --- a/python/tests/util_tests/test_cthread_pool.py +++ /dev/null @@ -1,48 +0,0 @@ -import ctypes -import ecl -from ecl import EclPrototype -from tests import EclTest -from ecl.util.util import CThreadPool, startCThreadPool - -TEST_LIB = EclPrototype.lib - - -class CThreadPoolTest(EclTest): - def test_cfunc(self): - with self.assertRaises(TypeError): - func = CThreadPool.lookupCFunction("WRONG-TYPE", "no-this-does-not-exist") - - with self.assertRaises(AttributeError): - func = CThreadPool.lookupCFunction(TEST_LIB, "no-this-does-not-exist") - - def test_create(self): - pool = CThreadPool(32, start=True) - job = CThreadPool.lookupCFunction(TEST_LIB, "thread_pool_test_func1") - arg = ctypes.c_int(0) - - N = 256 - for i in range(N): - pool.addTask(job, ctypes.byref(arg)) - pool.join() - self.assertEqual(arg.value, N) - - def test_context(self): - N = 256 - arg = ctypes.c_int(0) - job = CThreadPool.lookupCFunction(TEST_LIB, "thread_pool_test_func1") - with startCThreadPool(16) as tp: - for i in range(N): - tp.addTask(job, ctypes.byref(arg)) - self.assertEqual(arg.value, N) - - def test_add_task_function(self): - pool = CThreadPool(32, start=True) - pool.addTaskFunction("testFunction", TEST_LIB, "thread_pool_test_func1") - - arg = ctypes.c_int(0) - task_count = 256 - for i in range(task_count): - pool.testFunction(ctypes.byref(arg)) - - pool.join() - self.assertEqual(arg.value, task_count)