+
+LTTNG_HIDDEN
+pthread_attr_t *default_pthread_attr(void)
+{
+ int ret = 0;
+ size_t ptstacksize;
+ struct rlimit rlim;
+
+ pthread_mutex_lock(&tattr_lock);
+
+ /* Return cached value. */
+ if (pthread_attr_init_done) {
+ goto end;
+ }
+
+ /* Get system stack size limits. */
+ ret = getrlimit(RLIMIT_STACK, &rlim);
+ if (ret < 0) {
+ PERROR("getrlimit");
+ goto error;
+ }
+ DBG("Stack size limits: soft %lld, hard %lld bytes",
+ (long long) rlim.rlim_cur,
+ (long long) rlim.rlim_max);
+
+ /* Get pthread default thread stack size. */
+ ret = pthread_attr_getstacksize(&tattr, &ptstacksize);
+ if (ret < 0) {
+ PERROR("pthread_attr_getstacksize");
+ goto error;
+ }
+ DBG("Default pthread stack size is %zu bytes", ptstacksize);
+
+ /* Check if the default pthread stack size honors ulimits. */
+ if (ptstacksize < rlim.rlim_cur) {
+ DBG("Your libc doesn't honor stack size limits, setting thread stack size to soft limit (%lld bytes)",
+ (long long) rlim.rlim_cur);
+
+ /* Create pthread_attr_t struct with ulimit stack size. */
+ ret = pthread_attr_setstacksize(&tattr, rlim.rlim_cur);
+ if (ret < 0) {
+ PERROR("pthread_attr_setstacksize");
+ goto error;
+ }
+ }
+
+ /* Enable cached value. */
+ pthread_attr_init_done = true;
+end:
+ pthread_mutex_unlock(&tattr_lock);
+ return &tattr;
+error:
+ pthread_mutex_unlock(&tattr_lock);
+ WARN("Failed to initialize pthread attributes, using libc defaults.");
+ return NULL;
+}