Diff
Not logged in

Differences From Artifact [5d916dcebb]:

To Artifact [4d87d14e09]:


523
524
525
526
527
528
529



530
531
532
533
534
535
536
537
538
539

















540
541

542
543
544
545
546
547
548
549
550
551







552
553
554
555
556
557
558
559
560
561
562

563
564
565
566
567
568
569
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598







+
+
+










+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+










+
+
+
+
+
+
+











+







    Tcl_Condition *condPtr,	/* Really (pthread_cond_t **) */
    Tcl_Mutex *mutexPtr,	/* Really (pthread_mutex_t **) */
    const Tcl_Time *timePtr) /* Timeout on waiting period */
{
    pthread_cond_t *pcondPtr;
    pthread_mutex_t *pmutexPtr;
    struct timespec ptime;
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
    int *monoFlagPtr;
#endif

    if (*condPtr == NULL) {
	MASTER_LOCK;

	/*
	 * Double check inside mutex to avoid race, then initialize condition
	 * variable if necessary.
	 */

	if (*condPtr == NULL) {
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
	    pthread_condattr_t attr;

	    pcondPtr = ckalloc(sizeof(pthread_cond_t) + sizeof(int));
	    monoFlagPtr = (int *) (pcondPtr + 1);
	    pthread_condattr_init(&attr);
	    *monoFlagPtr = (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) == 0);
	    if (*monoFlagPtr) {
		if (pthread_cond_init(pcondPtr, &attr)) {
		    *monoFlagPtr = 0;
		    pthread_cond_init(pcondPtr, NULL);
		}
	    } else {
		pthread_cond_init(pcondPtr, NULL);
	    }
	    pthread_condattr_destroy(&attr);
#else
	    pcondPtr = ckalloc(sizeof(pthread_cond_t));
	    pthread_cond_init(pcondPtr, NULL);
#endif
	    *condPtr = (Tcl_Condition) pcondPtr;
	    TclRememberCondition(condPtr);
	}
	MASTER_UNLOCK;
    }
    pmutexPtr = *((pthread_mutex_t **)mutexPtr);
    pcondPtr = *((pthread_cond_t **)condPtr);
    if (timePtr == NULL) {
	pthread_cond_wait(pcondPtr, pmutexPtr);
    } else {
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
	monoFlagPtr = (int *) (pcondPtr + 1);
	clock_gettime(*monoFlagPtr ? CLOCK_MONOTONIC : CLOCK_REALTIME, &ptime);
	ptime.tv_sec += timePtr->sec +
	    (timePtr->usec * 1000 + ptime.tv_nsec) / 1000000000;
	ptime.tv_nsec = (timePtr->usec * 1000 + ptime.tv_nsec) % 1000000000;
#else
	Tcl_Time now;

	/*
	 * Make sure to take into account the microsecond component of the
	 * current time, including possible overflow situations. [Bug #411603]
	 */

	Tcl_GetTime(&now);
	ptime.tv_sec = timePtr->sec + now.sec +
	    (timePtr->usec + now.usec) / 1000000;
	ptime.tv_nsec = 1000 * ((timePtr->usec + now.usec) % 1000000);
#endif
	pthread_cond_timedwait(pcondPtr, pmutexPtr, &ptime);
    }
}

/*
 *----------------------------------------------------------------------
 *
809
810
811
812
813
814
815
816



817
























































































818
819
820
821
822
823
838
839
840
841
842
843
844

845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942







-
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+






{
    pthread_key_t *ptkeyPtr = tsdKeyPtr;

    return pthread_getspecific(*ptkeyPtr);
}

#endif /* TCL_THREADS */



#ifdef ANDROID
/*
 *----------------------------------------------------------------------
 *
 * TclpCondattrSetClock, TclpCondTimedwait --
 *
 *	Replacements for pthread_condattr_setclock() and
 *	pthread_cond_timedwait() on Android due to varying support
 *	depending on API level. Deals with the cases:
 *
 *	- API<21 has no pthread_condattr_setclock() but could have support
 *	  for pthread_cond_timedwait_monotonic_np().
 *
 *	- API>=21 has pthread_condattr_setclock() and doesn't require to
 *	  use pthread_cond_timedwait_monotonic_np() since
 *	  pthread_cond_timedwait() should deal with CLOCK_MONOTONIC cleanly.
 *
 *	This can be resolved only by runtime linking libc.so and
 *	using dlsym() to find out which pthread functions are available.
 *
 *----------------------------------------------------------------------
 */

#include <dlfcn.h>

typedef int (CondattrSetclockProc)(
    const pthread_condattr_t *attrPtr,
    clockid_t clkid);

typedef int (CondTimedwaitProc)(
    pthread_cond_t *condPtr,
    pthread_mutex_t *mutexPtr,
    struct timespec *tsPtr);

static CondattrSetclockProc *condattrSetclockProc = NULL;
static CondTimedwaitProc *condTimedwaitMonotonicNpProc = NULL;
static CondTimedwaitProc *condTimedwaitProc = NULL;

int TclpCondattrSetclock(
    const pthread_condattr_t *attrPtr,
    clockid_t clkid)
{
    if (condattrSetclockProc == NULL) {
	void *libc = dlopen("libc.so", RTLD_NOW | RTLD_GLOBAL);

	if (libc == NULL) {
	    /* no way to continue, thus ... */
	    Tcl_Panic("could not dlopen libc.so");
	}
	condTimedwaitProc = (CondTimedwaitProc *)
		dlsym(libc, "pthread_cond_timedwait");
	condattrSetclockProc = (CondattrSetclockProc *)
		dlsym(libc, "pthread_condattr_setclock");
	if (condattrSetclockProc != NULL) {
	    condTimedwaitMonotonicNpProc = (CondTimedwaitProc *) -1;
	} else {
	    condTimedwaitMonotonicNpProc = (CondTimedwaitProc *)
		    dlsym(libc, "pthread_cond_timedwait_monotonic_np");
	    if (condTimedwaitMonotonicNpProc == NULL) {
		condTimedwaitMonotonicNpProc = (CondTimedwaitProc *) -1;
	    }
	    condattrSetclockProc = (CondattrSetclockProc *) -1;
	}
	dlclose(libc);
    }
    if (condattrSetclockProc != (CondattrSetclockProc *) -1) {
	return condattrSetclockProc(attrPtr, clkid);
    }
    if (condTimedwaitMonotonicNpProc != (CondTimedwaitProc *) -1) {
	return 0;
    }
    return EINVAL;
}

int TclpCondTimedwait(
    pthread_cond_t *condPtr,
    pthread_mutex_t *mutexPtr,
    struct timespec *tsPtr)
{
    if ((condTimedwaitMonotonicNpProc != NULL) &&
	(condTimedwaitMonotonicNpProc != (CondTimedwaitProc *) -1)) {
	return condTimedwaitMonotonicNpProc(condPtr, mutexPtr, tsPtr);
    }
    return condTimedwaitProc(condPtr, mutexPtr, tsPtr);
}

#endif


/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */