Skip to content
Snippets Groups Projects
Commit 93d06fc9 authored by Konstantin Osipov's avatar Konstantin Osipov
Browse files

Storinig exceptions in TLS: code review.

parent 4ba8626a
No related branches found
No related tags found
No related merge requests found
......@@ -340,9 +340,9 @@ lbox_raise(lua_State *L)
{
if (lua_gettop(L) == 0) {
/* re-throw saved exceptions (if any) */
if (cord()->exc == NULL)
if (cord()->exception == NULL)
return 0;
cord()->exc->raise();
cord()->exception->raise();
return 0;
}
......
......@@ -35,38 +35,42 @@
#include <errno.h>
/* Statically allocate out-of-memory exception */
static ClientError out_of_memory(__FILE__, __LINE__, ER_MEMORY_ISSUE, 0,
ClientError out_of_memory(__FILE__, __LINE__, ER_MEMORY_ISSUE, 0,
"exception", "new");
void*
void *
Exception::operator new(size_t size)
{
/* Explicity call destructor for previous exception */
if (cord()->exc)
cord()->exc->~Exception();
/* Reuse memory allocated for exception */
if (cord()->exc_size < size) {
size_t new_size = cord()->exc_size > 0 ? cord()->exc_size : 512;
while (new_size < size)
new_size *= 2;
void *exc = realloc(cord()->exc, new_size);
if (exc == NULL)
throw &out_of_memory;
cord()->exc = (Exception *) exc;
cord()->exc_size = new_size;
}
struct cord *cord = cord();
return cord()->exc;
if (cord->exception == &out_of_memory) {
assert(cord->exception_size == 0);
cord->exception = NULL;
}
if (cord->exception) {
/* Explicitly call destructor for previous exception */
cord->exception->~Exception();
if (cord->exception_size >= size) {
/* Reuse memory allocated for exception */
return cord->exception;
}
free(cord->exception);
}
cord->exception = (ClientError *) malloc(size);
if (cord->exception) {
cord->exception_size = size;
return cord->exception;
}
cord->exception = &out_of_memory;
cord->exception_size = 0;
throw cord->exception;
}
void
Exception::operator delete(void*)
Exception::operator delete(void * /* ptr */)
{
/* Free memory allocated for exception */
free(cord()->exc);
cord()->exc = NULL;
cord()->exc_size = 0;
/* Unsupported */
assert(false);
}
Exception::Exception(const char *file, unsigned line)
......
......@@ -33,12 +33,14 @@
#include "errcode.h"
#include "say.h"
class Exception: public Object {
public:
void *operator new(size_t size);
void operator delete(void*);
virtual void raise()
{
/* Throw the most specific type of exception */
throw this;
}
......@@ -119,6 +121,8 @@ class ClientError: public Exception {
int m_errcode;
};
extern ClientError out_of_memory;
class LoggedError: public ClientError {
public:
template <typename ... Args>
......
......@@ -527,8 +527,10 @@ cord_destroy(struct cord *cord)
slab_cache_destroy(&cord->slabc);
ev_loop_destroy(cord->loop);
/* Cleanup memory allocated for exceptions */
if (cord->exc)
delete cord->exc;
if (cord->exception && cord->exception != &out_of_memory) {
cord->exception->~Exception();
free(cord->exception);
}
}
struct cord_thread_arg
......
......@@ -153,8 +153,8 @@ struct cord {
struct slab_cache slabc;
char name[FIBER_NAME_MAX];
/** Last thrown exception */
struct Exception *exc;
size_t exc_size;
struct Exception *exception;
size_t exception_size;
};
extern __thread struct cord *cord_ptr;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment