为什么 makecontext 的函数只接受整数参数
Why makecontext's func only accepts integer arguments
makecontext
的手册页指出 argc
之后的参数只能是整数 (int):
...the function func is called, and passed the series of integer (int) arguments that follow argc
如果我们查看 pthread_create
或 clone
系统调用,它们有一个 void*
参数要传递给 func,并且结构指针可以包含任意数量的用户希望拥有的数据,这两个函数也不需要使用va_args。所以我的问题是为什么 makecontext
不使用这种技术,即使用 void 指针,而不是 argc 和 va_args?
这是一个设计缺陷,导致了 makecontext
的 demise/removal。请参阅 POSIX 最新版本的合理文本,它包含在:
http://pubs.opengroup.org/onlinepubs/009695399/functions/makecontext.html
With the incorporation of the ISO/IEC 9899:1999 standard into this specification it was found that the ISO C standard (Subclause 6.11.6) specifies that the use of function declarators with empty parentheses is an obsolescent feature. Therefore, using the function prototype:
void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...);
is making use of an obsolescent feature of the ISO C standard. Therefore, a strictly conforming POSIX application cannot use this form. Therefore, use of getcontext(), makecontext(), and swapcontext() is marked obsolescent.
There is no way in the ISO C standard to specify a non-obsolescent function prototype indicating that a function will be called with an arbitrary number (including zero) of arguments of arbitrary types (including integers, pointers to data, pointers to functions, and composite types).
Replacing makecontext() with a number of ISO C standard-compatible functions handing various numbers and types of arguments would have forced all existing uses of makecontext() to be rewritten for little or no gain. There are very few applications today that use the *context() routines. Those that do use them are almost always using them to implement co-routines. By maintaining the XSH, Issue 5 specification for makecontext(), existing applications will continue to work, although they won't be able to be classified as strictly conforming applications.
There is no way in the ISO C standard (without using obsolescent behavior) to specify functionality that was standard, strictly conforming behavior in the XSH, Issue 5 specification using the ISO C standard. Threads can be used to implement the functionality provided by makecontext(), getcontext(), and swapcontext() but they are more complex to use. It was felt inventing new ISO C standard-compatible interfaces that describe what can be done with the XSH, Issue 5 functions and then converting applications to use them would cause more difficulty than just converting applications that use them to use threads instead.
请注意,为了让 makecontext
的实现真正调用 func
,它必须知道如何设置堆栈帧以进入具有正确编号和类型的函数争论。如果要求所有参数具有相同的类型 (int
),则可以仅从数字 argc
知道这一点。如果允许它们具有不同的类型,调用者将不得不以某种方式将类型规范作为参数传递给 makecontext
;一个简单的计数不足以知道如何将它们从 makecontext
接收到的 va_list
复制到堆栈帧以进入 func
.
当然makecontext
的func
应该只拿一个void *
,就不会出现这个问题了。但是没有。
makecontext
的手册页指出 argc
之后的参数只能是整数 (int):
...the function func is called, and passed the series of integer (int) arguments that follow argc
如果我们查看 pthread_create
或 clone
系统调用,它们有一个 void*
参数要传递给 func,并且结构指针可以包含任意数量的用户希望拥有的数据,这两个函数也不需要使用va_args。所以我的问题是为什么 makecontext
不使用这种技术,即使用 void 指针,而不是 argc 和 va_args?
这是一个设计缺陷,导致了 makecontext
的 demise/removal。请参阅 POSIX 最新版本的合理文本,它包含在:
http://pubs.opengroup.org/onlinepubs/009695399/functions/makecontext.html
With the incorporation of the ISO/IEC 9899:1999 standard into this specification it was found that the ISO C standard (Subclause 6.11.6) specifies that the use of function declarators with empty parentheses is an obsolescent feature. Therefore, using the function prototype:
void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...);
is making use of an obsolescent feature of the ISO C standard. Therefore, a strictly conforming POSIX application cannot use this form. Therefore, use of getcontext(), makecontext(), and swapcontext() is marked obsolescent.
There is no way in the ISO C standard to specify a non-obsolescent function prototype indicating that a function will be called with an arbitrary number (including zero) of arguments of arbitrary types (including integers, pointers to data, pointers to functions, and composite types).
Replacing makecontext() with a number of ISO C standard-compatible functions handing various numbers and types of arguments would have forced all existing uses of makecontext() to be rewritten for little or no gain. There are very few applications today that use the *context() routines. Those that do use them are almost always using them to implement co-routines. By maintaining the XSH, Issue 5 specification for makecontext(), existing applications will continue to work, although they won't be able to be classified as strictly conforming applications.
There is no way in the ISO C standard (without using obsolescent behavior) to specify functionality that was standard, strictly conforming behavior in the XSH, Issue 5 specification using the ISO C standard. Threads can be used to implement the functionality provided by makecontext(), getcontext(), and swapcontext() but they are more complex to use. It was felt inventing new ISO C standard-compatible interfaces that describe what can be done with the XSH, Issue 5 functions and then converting applications to use them would cause more difficulty than just converting applications that use them to use threads instead.
请注意,为了让 makecontext
的实现真正调用 func
,它必须知道如何设置堆栈帧以进入具有正确编号和类型的函数争论。如果要求所有参数具有相同的类型 (int
),则可以仅从数字 argc
知道这一点。如果允许它们具有不同的类型,调用者将不得不以某种方式将类型规范作为参数传递给 makecontext
;一个简单的计数不足以知道如何将它们从 makecontext
接收到的 va_list
复制到堆栈帧以进入 func
.
当然makecontext
的func
应该只拿一个void *
,就不会出现这个问题了。但是没有。