| 767 | } |
| 768 | |
| 769 | int |
| 770 | pthread_key_create (pthread_key_t *key, void (* dest)(void *)) |
| 771 | { |
| 772 | unsigned int i; |
| 773 | long nmax; |
| 774 | void (**d)(void *); |
| 775 | |
| 776 | if (!key) |
| 777 | return EINVAL; |
| 778 | |
| 779 | pthread_rwlock_wrlock (&_pthread_key_lock); |
| 780 | |
| 781 | for (i = _pthread_key_sch; i < _pthread_key_max; i++) |
| 782 | { |
| 783 | if (!_pthread_key_dest[i]) |
| 784 | { |
| 785 | *key = i; |
| 786 | if (dest) |
| 787 | _pthread_key_dest[i] = dest; |
| 788 | else |
| 789 | _pthread_key_dest[i] = (void(*)(void *))1; |
| 790 | pthread_rwlock_unlock (&_pthread_key_lock); |
| 791 | return 0; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | for (i = 0; i < _pthread_key_sch; i++) |
| 796 | { |
| 797 | if (!_pthread_key_dest[i]) |
| 798 | { |
| 799 | *key = i; |
| 800 | if (dest) |
| 801 | _pthread_key_dest[i] = dest; |
| 802 | else |
| 803 | _pthread_key_dest[i] = (void(*)(void *))1; |
| 804 | pthread_rwlock_unlock (&_pthread_key_lock); |
| 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | if (_pthread_key_max == PTHREAD_KEYS_MAX) |
| 811 | { |
| 812 | pthread_rwlock_unlock(&_pthread_key_lock); |
| 813 | return ENOMEM; |
| 814 | } |
| 815 | |
| 816 | nmax = _pthread_key_max * 2; |
| 817 | if (nmax == 0) |
| 818 | nmax = _pthread_key_max + 1; |
| 819 | if (nmax > PTHREAD_KEYS_MAX) |
| 820 | nmax = PTHREAD_KEYS_MAX; |
| 821 | |
| 822 | /* No spare room anywhere */ |
| 823 | d = (void (__cdecl **)(void *))realloc(_pthread_key_dest, nmax * sizeof(*d)); |
| 824 | if (!d) |
| 825 | { |
| 826 | pthread_rwlock_unlock (&_pthread_key_lock); |
no test coverage detected