提交 fb46be03 编写于 作者: A Alessandro Ghedini 提交者: Rich Salz

Convert CRYPTO_LOCK_BIO to new multi-threading API

Reviewed-by: NMatt Caswell <matt@openssl.org>
Reviewed-by: NRich Salz <rsalz@openssl.org>
上级 c9aad4ff
...@@ -94,12 +94,22 @@ int BIO_set(BIO *bio, BIO_METHOD *method) ...@@ -94,12 +94,22 @@ int BIO_set(BIO *bio, BIO_METHOD *method)
bio->num_read = 0L; bio->num_read = 0L;
bio->num_write = 0L; bio->num_write = 0L;
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
if (method->create != NULL)
bio->lock = CRYPTO_THREAD_lock_new();
if (bio->lock == NULL) {
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
return 0;
}
if (method->create != NULL) {
if (!method->create(bio)) { if (!method->create(bio)) {
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
return (0); CRYPTO_THREAD_lock_free(bio->lock);
return 0;
} }
return (1); }
return 1;
} }
int BIO_free(BIO *a) int BIO_free(BIO *a)
...@@ -107,23 +117,29 @@ int BIO_free(BIO *a) ...@@ -107,23 +117,29 @@ int BIO_free(BIO *a)
int i; int i;
if (a == NULL) if (a == NULL)
return (0); return 0;
if (CRYPTO_atomic_add(&a->references, -1, &i, a->lock) <= 0)
return 0;
i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_BIO);
REF_PRINT_COUNT("BIO", a); REF_PRINT_COUNT("BIO", a);
if (i > 0) if (i > 0)
return (1); return 1;
REF_ASSERT_ISNT(i < 0); REF_ASSERT_ISNT(i < 0);
if ((a->callback != NULL) && if ((a->callback != NULL) &&
((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0)) ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
return (i); return i;
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
CRYPTO_THREAD_lock_free(a->lock);
if ((a->method != NULL) && (a->method->destroy != NULL)) if ((a->method != NULL) && (a->method->destroy != NULL))
a->method->destroy(a); a->method->destroy(a);
OPENSSL_free(a); OPENSSL_free(a);
return (1);
return 1;
} }
void BIO_vfree(BIO *a) void BIO_vfree(BIO *a)
...@@ -131,6 +147,18 @@ void BIO_vfree(BIO *a) ...@@ -131,6 +147,18 @@ void BIO_vfree(BIO *a)
BIO_free(a); BIO_free(a);
} }
int BIO_up_ref(BIO *a)
{
int i;
if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
return 0;
REF_PRINT_COUNT("BIO", a);
REF_ASSERT_ISNT(i < 2);
return ((i > 1) ? 1 : 0);
}
void BIO_clear_flags(BIO *b, int flags) void BIO_clear_flags(BIO *b, int flags)
{ {
b->flags &= ~flags; b->flags &= ~flags;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
=head1 NAME =head1 NAME
BIO_new, BIO_set, BIO_free, BIO_vfree, BIO_free_all - BIO allocation and freeing functions BIO_new, BIO_set, BIO_up_ref, BIO_free, BIO_vfree, BIO_free_all - BIO allocation and freeing functions
=head1 SYNOPSIS =head1 SYNOPSIS
...@@ -10,6 +10,7 @@ BIO_new, BIO_set, BIO_free, BIO_vfree, BIO_free_all - BIO allocation and freeing ...@@ -10,6 +10,7 @@ BIO_new, BIO_set, BIO_free, BIO_vfree, BIO_free_all - BIO allocation and freeing
BIO * BIO_new(BIO_METHOD *type); BIO * BIO_new(BIO_METHOD *type);
int BIO_set(BIO *a,BIO_METHOD *type); int BIO_set(BIO *a,BIO_METHOD *type);
int BIO_up_ref(BIO *a);
int BIO_free(BIO *a); int BIO_free(BIO *a);
void BIO_vfree(BIO *a); void BIO_vfree(BIO *a);
void BIO_free_all(BIO *a); void BIO_free_all(BIO *a);
...@@ -20,6 +21,8 @@ The BIO_new() function returns a new BIO using method B<type>. ...@@ -20,6 +21,8 @@ The BIO_new() function returns a new BIO using method B<type>.
BIO_set() sets the method of an already existing BIO. BIO_set() sets the method of an already existing BIO.
BIO_up_ref() increments the reference count associated with the BIO object.
BIO_free() frees up a single BIO, BIO_vfree() also frees up a single BIO BIO_free() frees up a single BIO, BIO_vfree() also frees up a single BIO
but it does not return a value. but it does not return a value.
If B<a> is NULL nothing is done. If B<a> is NULL nothing is done.
...@@ -36,7 +39,7 @@ If B<a> is NULL nothing is done. ...@@ -36,7 +39,7 @@ If B<a> is NULL nothing is done.
BIO_new() returns a newly created BIO or NULL if the call fails. BIO_new() returns a newly created BIO or NULL if the call fails.
BIO_set(), BIO_free() return 1 for success and 0 for failure. BIO_set(), BIO_up_ref() and BIO_free() return 1 for success and 0 for failure.
BIO_free_all() and BIO_vfree() do not return values. BIO_free_all() and BIO_vfree() do not return values.
......
...@@ -326,6 +326,7 @@ struct bio_st { ...@@ -326,6 +326,7 @@ struct bio_st {
uint64_t num_read; uint64_t num_read;
uint64_t num_write; uint64_t num_write;
CRYPTO_EX_DATA ex_data; CRYPTO_EX_DATA ex_data;
CRYPTO_RWLOCK *lock;
}; };
DEFINE_STACK_OF(BIO) DEFINE_STACK_OF(BIO)
...@@ -635,6 +636,7 @@ BIO *BIO_new(BIO_METHOD *type); ...@@ -635,6 +636,7 @@ BIO *BIO_new(BIO_METHOD *type);
int BIO_set(BIO *a, BIO_METHOD *type); int BIO_set(BIO *a, BIO_METHOD *type);
int BIO_free(BIO *a); int BIO_free(BIO *a);
void BIO_vfree(BIO *a); void BIO_vfree(BIO *a);
int BIO_up_ref(BIO *a);
int BIO_read(BIO *b, void *data, int len); int BIO_read(BIO *b, void *data, int len);
int BIO_gets(BIO *bp, char *buf, int size); int BIO_gets(BIO *bp, char *buf, int size);
int BIO_write(BIO *b, const void *data, int len); int BIO_write(BIO *b, const void *data, int len);
......
...@@ -181,7 +181,6 @@ extern "C" { ...@@ -181,7 +181,6 @@ extern "C" {
# define CRYPTO_LOCK_RAND 18 # define CRYPTO_LOCK_RAND 18
# define CRYPTO_LOCK_RAND2 19 # define CRYPTO_LOCK_RAND2 19
# define CRYPTO_LOCK_MALLOC 20 # define CRYPTO_LOCK_MALLOC 20
# define CRYPTO_LOCK_BIO 21
# define CRYPTO_LOCK_READDIR 24 # define CRYPTO_LOCK_READDIR 24
# define CRYPTO_LOCK_RSA_BLINDING 25 # define CRYPTO_LOCK_RSA_BLINDING 25
# define CRYPTO_LOCK_MALLOC2 27 # define CRYPTO_LOCK_MALLOC2 27
......
...@@ -338,7 +338,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) ...@@ -338,7 +338,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
if (b->next_bio != NULL) if (b->next_bio != NULL)
BIO_push(bio, b->next_bio); BIO_push(bio, b->next_bio);
b->next_bio = bio; b->next_bio = bio;
CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO); BIO_up_ref(bio);
} }
b->init = 1; b->init = 1;
break; break;
...@@ -371,7 +371,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) ...@@ -371,7 +371,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
case BIO_CTRL_PUSH: case BIO_CTRL_PUSH:
if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) { if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) {
SSL_set_bio(ssl, b->next_bio, b->next_bio); SSL_set_bio(ssl, b->next_bio, b->next_bio);
CRYPTO_add(&b->next_bio->references, 1, CRYPTO_LOCK_BIO); BIO_up_ref(b);
} }
break; break;
case BIO_CTRL_POP: case BIO_CTRL_POP:
...@@ -384,7 +384,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) ...@@ -384,7 +384,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
if (ssl->rbio != ssl->wbio) if (ssl->rbio != ssl->wbio)
BIO_free_all(ssl->wbio); BIO_free_all(ssl->wbio);
if (b->next_bio != NULL) if (b->next_bio != NULL)
CRYPTO_add(&b->next_bio->references, -1, CRYPTO_LOCK_BIO); BIO_free(b->next_bio);
ssl->wbio = NULL; ssl->wbio = NULL;
ssl->rbio = NULL; ssl->rbio = NULL;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册