LCOV - code coverage report
Current view: top level - core/support - thd_posix.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 21 22 95.5 %
Date: 2015-12-10 22:15:08 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : /* Posix implementation for gpr threads. */
      35             : 
      36             : #include <grpc/support/port_platform.h>
      37             : 
      38             : #ifdef GPR_POSIX_SYNC
      39             : 
      40             : #include <pthread.h>
      41             : #include <stdlib.h>
      42             : #include <string.h>
      43             : #include <grpc/support/alloc.h>
      44             : #include <grpc/support/log.h>
      45             : #include <grpc/support/thd.h>
      46             : #include <grpc/support/useful.h>
      47             : 
      48             : struct thd_arg {
      49             :   void (*body)(void *arg); /* body of a thread */
      50             :   void *arg;               /* argument to a thread */
      51             : };
      52             : 
      53             : /* Body of every thread started via gpr_thd_new. */
      54       39539 : static void *thread_body(void *v) {
      55       39539 :   struct thd_arg a = *(struct thd_arg *)v;
      56       39539 :   free(v);
      57       39539 :   (*a.body)(a.arg);
      58       39535 :   return NULL;
      59             : }
      60             : 
      61       39539 : int gpr_thd_new(gpr_thd_id *t, void (*thd_body)(void *arg), void *arg,
      62             :                 const gpr_thd_options *options) {
      63             :   int thread_started;
      64             :   pthread_attr_t attr;
      65             :   pthread_t p;
      66             :   /* don't use gpr_malloc as we may cause an infinite recursion with
      67             :    * the profiling code */
      68       39539 :   struct thd_arg *a = malloc(sizeof(*a));
      69       39539 :   GPR_ASSERT(a != NULL);
      70       39539 :   a->body = thd_body;
      71       39539 :   a->arg = arg;
      72             : 
      73       39539 :   GPR_ASSERT(pthread_attr_init(&attr) == 0);
      74       39539 :   if (gpr_thd_options_is_detached(options)) {
      75       27922 :     GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) ==
      76             :                0);
      77             :   } else {
      78       11617 :     GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) ==
      79             :                0);
      80             :   }
      81       39539 :   thread_started = (pthread_create(&p, &attr, &thread_body, a) == 0);
      82       39539 :   GPR_ASSERT(pthread_attr_destroy(&attr) == 0);
      83       39539 :   if (!thread_started) {
      84           0 :     free(a);
      85             :   }
      86       39539 :   *t = (gpr_thd_id)p;
      87       39539 :   return thread_started;
      88             : }
      89             : 
      90      808715 : gpr_thd_id gpr_thd_currentid(void) { return (gpr_thd_id)pthread_self(); }
      91             : 
      92       11615 : void gpr_thd_join(gpr_thd_id t) { pthread_join((pthread_t)t, NULL); }
      93             : 
      94             : #endif /* GPR_POSIX_SYNC */

Generated by: LCOV version 1.11