proxy_C_Initialize (CK_X_FUNCTION_LIST *self,
                    CK_VOID_PTR init_args)
{
	State *state = (State *)self;
	bool initialize = false;
	Mapping *mappings = NULL;
	unsigned int n_mappings = 0;
	Proxy *py;
	CK_RV rv;

	p11_library_init_once ();

	/* WARNING: This function must be reentrant */

	p11_debug ("in");

	p11_lock ();

		if (!PROXY_VALID (state->px)) {
			unsigned call_finalize = 1;

			initialize = true;
			if (PROXY_FORKED(state->px)) {
				call_finalize = 0;
				if (state->px->mappings) {
					mappings = state->px->mappings;
					n_mappings = state->px->n_mappings;
					state->px->mappings = NULL;
					state->px->n_mappings = 0;
				}
			}
			proxy_free (state->px, call_finalize);

			state->px = NULL;
		} else {
			state->px->refs++;
		}

	p11_unlock ();

	if (!initialize) {
		p11_debug ("out: already: %lu", CKR_OK);
		return CKR_OK;
	}

	rv = proxy_create (&py, state->loaded, mappings, n_mappings);
	free (mappings);
	if (rv != CKR_OK) {
		p11_debug ("out: %lu", rv);
		return rv;
	}

	p11_lock ();

		if (state->px == NULL) {
			state->px = py;
			py = NULL;
		}

	p11_unlock ();

	proxy_free (py, 1);
	p11_debug ("out: 0");
	return rv;
}
proxy_C_Initialize (CK_X_FUNCTION_LIST *self,
                    CK_VOID_PTR init_args)
{
	State *state = (State *)self;
	bool initialize = false;
	Mapping *mappings = NULL;
	unsigned int n_mappings = 0;
	Proxy *py;
	CK_RV rv;

	p11_library_init_once ();

	 

	p11_debug ("in");

	p11_lock ();

		if (!PROXY_VALID (state->px)) {
			unsigned call_finalize = 1;

			initialize = true;
			if (PROXY_FORKED(state->px)) {
				call_finalize = 0;
				if (state->px->mappings) {
					mappings = state->px->mappings;
					n_mappings = state->px->n_mappings;
					state->px->mappings = NULL;
					state->px->n_mappings = 0;
				}
			}
			proxy_free (state->px, call_finalize);

			state->px = NULL;
		} else {
			state->px->refs++;
		}

	p11_unlock ();

	if (!initialize) {
		p11_debug ("out: already: %lu", CKR_OK);
		return CKR_OK;
	}

	rv = proxy_create (&py, state->loaded, mappings, n_mappings);
	free (mappings);
	if (rv != CKR_OK) {
		p11_debug ("out: %lu", rv);
		return rv;
	}

	p11_lock ();

		if (state->px == NULL) {
			state->px = py;
			py = NULL;
		}

	p11_unlock ();

	proxy_free (py, 1);
	p11_debug ("out: 0");
	return rv;
}