"Fossies" - the Fresh Open Source Software Archive

Member "apr-1.7.0/test/testmutexscope.c" (4 Apr 2017, 6428 Bytes) of package /linux/www/apr-1.7.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "testmutexscope.c": 1.6.5_vs_1.7.0.

    1 /* Licensed to the Apache Software Foundation (ASF) under one or more
    2  * contributor license agreements.  See the NOTICE file distributed with
    3  * this work for additional information regarding copyright ownership.
    4  * The ASF licenses this file to You under the Apache License, Version 2.0
    5  * (the "License"); you may not use this file except in compliance with
    6  * the License.  You may obtain a copy of the License at
    7  *
    8  *     http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 /* This program won't run or check correctly if assert() is disabled. */
   18 #undef NDEBUG
   19 #include <assert.h>
   20 #include <errno.h>
   21 #include <stdio.h>
   22 #include <stdlib.h>
   23 
   24 #include "apr.h"
   25 #include "apr_general.h"
   26 #include "apr_proc_mutex.h"
   27 #include "apr_global_mutex.h"
   28 #include "apr_thread_proc.h"
   29 
   30 #if !APR_HAS_THREADS
   31 int main(void)
   32 {
   33     printf("This test requires APR thread support.\n");
   34     return 0;
   35 }
   36 
   37 #else /* APR_HAS_THREADS */
   38 
   39 static apr_thread_mutex_t *thread_mutex;
   40 static apr_proc_mutex_t *proc_mutex;
   41 static apr_global_mutex_t *global_mutex;
   42 static apr_pool_t *p;
   43 static volatile int counter;
   44 typedef enum {TEST_GLOBAL, TEST_PROC} test_mode_e;
   45 
   46 static int lock_init(apr_lockmech_e mech, test_mode_e test_mode)
   47 {
   48     apr_status_t rv;
   49     if (test_mode == TEST_PROC) {
   50         rv = apr_proc_mutex_create(&proc_mutex,
   51                                            NULL,
   52                                            mech,
   53                                            p);
   54     }
   55     else {
   56         rv = apr_global_mutex_create(&global_mutex,
   57                                      NULL,
   58                                      mech,
   59                                      p);
   60     }
   61     return rv;
   62 }
   63 
   64 static void lock_destroy(test_mode_e test_mode)
   65 {
   66     if (test_mode == TEST_PROC) {
   67         assert(apr_proc_mutex_destroy(proc_mutex) == APR_SUCCESS);
   68     }
   69     else {
   70         assert(apr_global_mutex_destroy(global_mutex) == APR_SUCCESS);
   71     }
   72 }
   73 
   74 static void lock_grab(test_mode_e test_mode)
   75 {
   76     if (test_mode == TEST_PROC) {
   77         assert(apr_proc_mutex_lock(proc_mutex) == APR_SUCCESS);
   78     }
   79     else {
   80         assert(apr_global_mutex_lock(global_mutex) == APR_SUCCESS);
   81     }
   82 }
   83 
   84 static void lock_release(test_mode_e test_mode)
   85 {
   86     if (test_mode == TEST_PROC) {
   87         assert(apr_proc_mutex_unlock(proc_mutex) == APR_SUCCESS);
   88     }
   89     else {
   90         assert(apr_global_mutex_unlock(global_mutex) == APR_SUCCESS);
   91     }
   92 }
   93 
   94 static void * APR_THREAD_FUNC eachThread(apr_thread_t *id, void *p)
   95 {
   96     test_mode_e test_mode = (test_mode_e)p;
   97 
   98     lock_grab(test_mode);
   99     ++counter;
  100     assert(apr_thread_mutex_lock(thread_mutex) == APR_SUCCESS);
  101     assert(apr_thread_mutex_unlock(thread_mutex) == APR_SUCCESS);
  102     lock_release(test_mode);
  103     apr_thread_exit(id, 0);
  104     return NULL;
  105 }
  106 
  107 static void test_mech_mode(apr_lockmech_e mech, const char *mech_name,
  108                            test_mode_e test_mode)
  109 {
  110   apr_thread_t *threads[20];
  111   int numThreads = 5;
  112   int i;
  113   apr_status_t rv;
  114 
  115   printf("Trying %s mutexes with mechanism `%s'...\n",
  116          test_mode == TEST_GLOBAL ? "global" : "proc", mech_name);
  117 
  118   assert(numThreads <= sizeof(threads) / sizeof(threads[0]));
  119 
  120   assert(apr_pool_create(&p, NULL) == APR_SUCCESS);
  121 
  122   assert(apr_thread_mutex_create(&thread_mutex, 0, p) == APR_SUCCESS);
  123   assert(apr_thread_mutex_lock(thread_mutex) == APR_SUCCESS);
  124   
  125   rv = lock_init(mech, test_mode);
  126   if (rv != APR_SUCCESS) {
  127       char errmsg[256];
  128       printf("%s mutexes with mechanism `%s': %s\n",
  129              test_mode == TEST_GLOBAL ? "Global" : "Proc", mech_name,
  130              apr_strerror(rv, errmsg, sizeof errmsg));
  131       if (rv != APR_ENOTIMPL || mech == APR_LOCK_DEFAULT) {
  132           exit(1);
  133       }
  134       return;
  135   }
  136 
  137   counter = 0;
  138 
  139   i = 0;
  140   while (i < numThreads)
  141   {
  142     rv = apr_thread_create(&threads[i],
  143                            NULL,
  144                            eachThread,
  145                            (void *)test_mode,
  146                            p);
  147     if (rv != APR_SUCCESS) {
  148       fprintf(stderr, "apr_thread_create->%d\n", rv);
  149       exit(1);
  150     }
  151     ++i;
  152   }
  153 
  154   apr_sleep(apr_time_from_sec(5));
  155 
  156   if (test_mode == TEST_PROC) {
  157       printf("  mutex mechanism `%s' is %sglobal in scope on this platform.\n",
  158              mech_name, counter == 1 ? "" : "*NOT* ");
  159   }
  160   else {
  161       if (counter != 1) {
  162           fprintf(stderr, "\n!!!apr_global_mutex operations are broken on this "
  163                   "platform for mutex mechanism `%s'!\n"
  164                   "They don't block out threads within the same process.\n",
  165                   mech_name);
  166           fprintf(stderr, "counter value: %d\n", counter);
  167           exit(1);
  168       }
  169       else {
  170           printf("  no problem encountered...\n");
  171       }
  172   }
  173   
  174   assert(apr_thread_mutex_unlock(thread_mutex) == APR_SUCCESS);
  175 
  176   i = 0;
  177   while (i < numThreads)
  178   {
  179     apr_status_t ignored;
  180 
  181     rv = apr_thread_join(&ignored,
  182                          threads[i]);
  183     assert(rv == APR_SUCCESS);
  184     ++i;
  185   }
  186 
  187   lock_destroy(test_mode);
  188   apr_thread_mutex_destroy(thread_mutex);
  189   apr_pool_destroy(p);
  190 }
  191 
  192 static void test_mech(apr_lockmech_e mech, const char *mech_name)
  193 {
  194     test_mech_mode(mech, mech_name, TEST_PROC);
  195     test_mech_mode(mech, mech_name, TEST_GLOBAL);
  196 }
  197 
  198 int main(void)
  199 {
  200     struct {
  201         apr_lockmech_e mech;
  202         const char *mech_name;
  203     } lockmechs[] = {
  204         {APR_LOCK_DEFAULT, "default"}
  205 #if APR_HAS_FLOCK_SERIALIZE
  206         ,{APR_LOCK_FLOCK, "flock"}
  207 #endif
  208 #if APR_HAS_SYSVSEM_SERIALIZE
  209         ,{APR_LOCK_SYSVSEM, "sysvsem"}
  210 #endif
  211 #if APR_HAS_POSIXSEM_SERIALIZE
  212         ,{APR_LOCK_POSIXSEM, "posix"}
  213 #endif
  214 #if APR_HAS_FCNTL_SERIALIZE
  215         ,{APR_LOCK_FCNTL, "fcntl"}
  216 #endif
  217 #if APR_HAS_PROC_PTHREAD_SERIALIZE
  218         ,{APR_LOCK_PROC_PTHREAD, "proc_pthread"}
  219 #endif
  220         ,{APR_LOCK_DEFAULT_TIMED, "default_timed"}
  221     };
  222     int i;
  223         
  224     assert(apr_initialize() == APR_SUCCESS);
  225 
  226     for (i = 0; i < sizeof(lockmechs) / sizeof(lockmechs[0]); i++) {
  227         test_mech(lockmechs[i].mech, lockmechs[i].mech_name);
  228     }
  229     
  230     apr_terminate();
  231     return 0;
  232 }
  233 
  234 #endif /* APR_HAS_THREADS */