Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions include/tscore/ink_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,31 @@

***********************************************************************/

#include "tscore/ink_defs.h"
#include "tscore/ink_error.h"
#include <tscore/ink_defs.h>
#include <tscore/ink_error.h>
#include <tscpp/util/Strerror.h>

#include <pthread.h>
#include <cstdlib>
typedef pthread_mutex_t ink_mutex;

void ink_mutex_init(ink_mutex *m);

// This will abort if the mutex is locked. Has better performance than ink_mutex_safer_destroy().
//
void ink_mutex_destroy(ink_mutex *m);

// In a relesae build, if the mutex is locked, this will log a warning, then wait up to 10 seconds for the mutex to be
// unlocked by another thread. In a debug build, the behavior is essentially the same as ink_mutex_destroy().
//
void ink_mutex_safer_destroy(ink_mutex *m);

static inline void
ink_mutex_acquire(ink_mutex *m)
{
int error = pthread_mutex_lock(m);
if (unlikely(error != 0)) {
ink_abort("pthread_mutex_lock(%p) failed: %s (%d)", m, strerror(error), error);
ink_abort("pthread_mutex_lock(%p) failed: %s (%d)", m, ts::Strerror(error).c_str(), error);
}
}

Expand All @@ -56,7 +65,7 @@ ink_mutex_release(ink_mutex *m)
{
int error = pthread_mutex_unlock(m);
if (unlikely(error != 0)) {
ink_abort("pthread_mutex_unlock(%p) failed: %s (%d)", m, strerror(error), error);
ink_abort("pthread_mutex_unlock(%p) failed: %s (%d)", m, ts::Strerror(error).c_str(), error);
}
}

Expand Down
1 change: 1 addition & 0 deletions include/tscpp/util/TsSharedMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#pragma once

#include <pthread.h>

#include <tscpp/util/Strerror.h>

#if __has_include(<ts/ts.h>)
Expand Down
2 changes: 1 addition & 1 deletion iocore/eventsystem/I_Lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ ProxyMutex::free()
print_lock_stats(1);
#endif
#endif
ink_mutex_destroy(&the_mutex);
ink_mutex_safer_destroy(&the_mutex);
mutexAllocator.free(this);
}

Expand Down
1 change: 1 addition & 0 deletions src/tscore/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ libtscore_la_SOURCES = \
ink_inet.cc \
ink_memory.cc \
ink_mutex.cc \
ink_mutex_sd.cc \
ink_queue.cc \
ink_queue_utils.cc \
ink_rand.cc \
Expand Down
12 changes: 5 additions & 7 deletions src/tscore/ink_mutex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
limitations under the License.
*/

#include "tscore/ink_error.h"
#include "tscore/ink_defs.h"
#include <cassert>
#include <cstdio>
#include "tscore/ink_mutex.h"
#include <tscore/ink_error.h>
#include <tscore/ink_mutex.h>
#include <tscpp/util/Strerror.h>

ink_mutex __global_death = PTHREAD_MUTEX_INITIALIZER;

Expand Down Expand Up @@ -58,7 +56,7 @@ ink_mutex_init(ink_mutex *m)

error = pthread_mutex_init(m, &attr.attr);
if (unlikely(error != 0)) {
ink_abort("pthread_mutex_init(%p) failed: %s (%d)", m, strerror(error), error);
ink_abort("pthread_mutex_init(%p) failed: %s (%d)", m, ts::Strerror(error).c_str(), error);
}
}

Expand All @@ -69,6 +67,6 @@ ink_mutex_destroy(ink_mutex *m)

error = pthread_mutex_destroy(m);
if (unlikely(error != 0)) {
ink_abort("pthread_mutex_destroy(%p) failed: %s (%d)", m, strerror(error), error);
ink_abort("pthread_mutex_destroy(%p) failed: %s (%d)", m, ts::Strerror(error).c_str(), error);
}
}
63 changes: 63 additions & 0 deletions src/tscore/ink_mutex_sd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/** @file

A brief file description

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <tscore/ink_error.h>
#include <tscore/ink_mutex.h>
#include <tscore/Diags.h>
#include <tscpp/util/Strerror.h>

// This is in a separate module from ink_mutex because some TS tools don't need this function, and also do not link in
// the Diags objects. So putting this in ink_mutex causes a link error for these tools for the Warning() call.

void
ink_mutex_safer_destroy(ink_mutex *m)
{
int error;

error = pthread_mutex_trylock(m);
if (unlikely(error != 0)) {
if (error != EBUSY) {
ink_abort("pthread_mutex_trylock(%p) failed: %s (%d)", m, ts::Strerror(error).c_str(), error);
} else {
#if DEBUG
ink_abort("destroying mutex (%p) that is still locked", m);
#else
Warning("ink_mutex_safer_destroy: destroying mutex (%p) that is still locked", m);
#endif

timespec timeout;
timeout.tv_sec = 10;
timeout.tv_nsec = 0;
error = pthread_mutex_timedlock(m, &timeout);
if (error != 0) {
ink_abort("pthread_mutex_trylock(%p) failed: %s (%d)", m, ts::Strerror(error).c_str(), error);
}
}
}
ink_mutex_release(m);

error = pthread_mutex_destroy(m);
if (unlikely(error != 0)) {
ink_abort("pthread_mutex_destroy(%p) failed: %s (%d)", m, ts::Strerror(error).c_str(), error);
}
}